如何仅对字符串的子部分执行字符串替换?

时间:2010-01-18 15:43:41

标签: c# string performance replace

我想要一种有效的方法,可以像这样工作

编辑:对不起,我没有把我以前尝试过的东西放进去。我现在更新了这个例子。

// Method signature, Only replaces first instance or how many are specified in max
public int MyReplace(ref string source,string org, string replace, int start, int max)
{
     int ret = 0;
     int len = replace.Length;
     int olen = org.Length;
     for(int i = 0; i < max; i++)
     {
          // Find the next instance of the search string
          int x = source.IndexOf(org, ret + olen);
          if(x > ret)
             ret = x;
          else
             break;

         // Insert the replacement
         source = source.Insert(x, replace);
         // And remove the original
         source = source.Remove(x + len, olen); // removes original string
     }
     return ret;
}

string source = "The cat can fly but only if he is the cat in the hat";
int i = MyReplace(ref source,"cat", "giraffe", 8, 1); 

// Results in the string "The cat can fly but only if he is the giraffe in the hat"
// i contains the index of the first letter of "giraffe" in the new string

我问的唯一原因是因为我的实现我想象有1000次替换会变慢。

5 个答案:

答案 0 :(得分:10)

怎么样:

public static int MyReplace(ref string source,
    string org, string replace, int start, int max)
{
    if (start < 0) throw new System.ArgumentOutOfRangeException("start");
    if (max <= 0) return 0;
    start = source.IndexOf(org, start);
    if (start < 0) return 0;
    StringBuilder sb = new StringBuilder(source, 0, start, source.Length);
    int found = 0;
    while (max-- > 0) {
        int index = source.IndexOf(org, start);
        if (index < 0) break;
        sb.Append(source, start, index - start).Append(replace);
        start = index + org.Length;
        found++;
    }
    sb.Append(source, start, source.Length - start);
    source = sb.ToString();
    return found;
}

它使用StringBuilder来避免大量中间string;我没有严格测试它,但它似乎工作。当没有匹配时,它还会尝试避免额外的string

答案 1 :(得分:1)

首先,尝试这样的事情:

int count = 0;
Regex.Replace(source, Regex.Escape(literal), (match) =>
{
    return (count++ > something) ? "new value" : match.Value;
});

答案 2 :(得分:0)

仅替换第一场比赛:

    private string ReplaceFirst(string source, string oldString, string newString)
    {
        var index = source.IndexOf(oldString);
        var begin = source.Substring(0, index);
        var end = source.Substring(index + oldString.Length);
        return begin + newString + end;
    }

答案 3 :(得分:0)

您有一个错误,如果它在开头,您将错过要替换的项目。

更改这些行;

  int ret = start;  // instead of zero, or you ignore the start parameter

  // Find the next instance of the search string
  // Do not skip olen for the first search!
  int x = i == 0 ? source.IndexOf(org, ret) : source.IndexOf(org, ret + olen);

此外,您的例行程序在我的机器上每秒更换30万次。你确定这会成为一个瓶颈吗?

如果您用较小的文本替换较大的文本,那么您的代码也会出现问题。

答案 4 :(得分:0)

如果您有四次替换,此代码速度提高100%,一次替换速度提高约10%(与发布的原始代码相比速度更快)。它使用指定的启动参数,并在用较小的文本替换较大的文本时起作用。

Mark Gravells解决方案(没有冒犯;-)作为原始代码慢60%,它还返回另一个值。

    // Method signature, Only replaces first instance or how many are specified in max
    public static int MyReplace(ref string source, string org, string replace, int    start, int max)
    {
        var ret = 0;
        int x = start;
        int reps = 0;
        int l = source.Length;
        int lastIdx = 0;
        string repstring = "";

        while (x < l)
        {
            if ((source[x] == org[0]) && (reps < max) && (x >= start))
            {
                bool match = true;
                for (int y = 1; y < org.Length; y++)
                {
                    if (source[x + y] != org[y])
                    {
                        match = false;
                        break;
                    }
                }
                if (match)
                {
                    repstring += source.Substring(lastIdx, x - lastIdx) + replace;
                    ret = x;
                    x += org.Length - 1;
                    reps++;
                    lastIdx = x + 1;
                    // Done?
                    if (reps == max)
                    {
                        source = repstring + source.Substring(lastIdx);
                        return ret;
                    }
                }
            }
            x++;
        }

        if (ret > 0)
        {
            source = repstring + source.Substring(lastIdx);
        }

        return ret;
    }