删除字符串的最佳方法是什么?

时间:2011-07-11 22:07:56

标签: c# .net string performance

我需要具有最佳性能的想法来删除/过滤字符串

我有:

string Input = "view('512', 3, 159);";

删除“view(”和“)的最佳性能方式是什么;”和报价? 我可以这样做:

Input = Input.Replace("view(","").Replace("'","").Replace("\"","").Replace(");",""); 

但似乎相当不优雅。

Input.Split('(')[1].Split(')')[0].Replace("'", "");

看起来好多了

我希望不使用正则表达式;我需要尽可能快地完成应用程序。 提前致谢! :)

10 个答案:

答案 0 :(得分:4)

您可以使用简单的linq语句:

string Input = "view('512', 3, 159);";

string output = new String( Input.Where( c => Char.IsDigit( c ) || c == ',' ).ToArray() );

输出:512,3,159

如果你想要空格,只需在where子句中添加一个检查。

答案 1 :(得分:2)

您只需使用Substring即可删除view();

Input.Substring(5, Input.Length - 7)

除此之外,它看起来相当有效。普通字符串操作已得到很好的优化。

所以:

Input =
  Input.Substring(5, Input.Length - 7)
  .Replace("'", String.Empty)
  .Replace("\"", String.Enmpty);

答案 2 :(得分:2)

char[] Output = Input.SkipWhile(x => x != '(') // skip before open paren
                     .Skip(1)                  // skip open paren
                     .TakeWhile(x => x != ')') // take everything until close paren
                     .Where(x => x != '\'' && x != '\"') // except quotes
                     .ToArray();
return new String(Output);

答案 3 :(得分:2)

希望这有帮助

Regex.Replace("view('512', 3, 159);",@"[(view)';]","")

答案 4 :(得分:1)

使用以下内容:

            System.Text.StringBuilder sb=new System.Text.StringBuilder();
        int state=0;
        for(var i=0;i<Input.Length;i++){
            switch(state){
                case 0: // beginning
                    if(Input[i]=='('){
                        state=1; // seen left parenthesis
                    }
                    break;
                case 2: // seen end parentheses
                    break; // ignore
                case 1:
                    if(Input[i]==')'){
                        state=2; // seen right parentheses
                    } else if(Input[i]!='\''){

                        sb.Append(Input[i]);
                    }
                    break;
            }
        }
        Console.WriteLine(sb.ToString());

答案 5 :(得分:1)

IndexOf,LastIndexOf和Substring可能最快。

string Input = "view('512', 3, 159);"; 
int p1 = Input.IndexOf('(');
int p2 = Input.LastIndexOf(')');
Input = Input.Substring (p1 + 1, p2 - p1 - 1);

答案 6 :(得分:1)

    var result = new string(Input.ToCharArray().
SkipWhile (i => i != '\'').
TakeWhile (i => i != ')').ToArray());

答案 7 :(得分:1)

为什么不想使用正则表达式?正则表达式经过了大量优化,并且比任何手写黑客都要快得多。

这是java(因为我运行linux而不能运行c#),但我希望你明白这一点。

input.replace("view(","").replace("'","").replace("\"","").replace(");",""); 

在我的电脑上,大约6秒内重复上述一百万次。然而,下面的正则表达式在大约2秒内运行。

// create java's regex matcher object
// matcher is looking for sequences of digits (valid integers)
Matcher matcher = Pattern.compile("(\\d+)").matcher(s);
StringBuilder builder = new StringBuilder();
// whilst we can find matches append the match plus a comma to a string builder
while (matcher.find()) {
    builder.append(matcher.group()).append(',');
}
// return the built string less the last trailing comma
return builder.substring(0, builder.length()-1);

如果要查找有效小数和整数,请改用以下模式。虽然它的运行速度比原来慢一点。

"(\\d+(\\.\\d*)?)"

答案 8 :(得分:0)

最快的方式是Input = Input.Substring(5, Input.Length - 7)

答案 9 :(得分:0)

更通用

void Main()
{
    string Input = "view('512', 3, 159);";
    var statingPoint = Input.IndexOf('(') + 1;

    var result = Input.Substring(statingPoint, Input.IndexOf(')') - statingPoint);
}