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

本文关键字:方法 是什么 字符串 删除 | 更新日期: 2023-09-27 18:01:52

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

我有:

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

删除"view(" and ");"和引号的最佳性能方法是什么?我可以这样做:

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

但是看起来很不优雅。

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

看起来好多了

我不想用正则表达式来做;我需要使更快的应用程序,我可以。提前感谢!:)

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

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

string Input = "view('512', 3, 159);";
string output = new String( Input.Where( c => Char.IsDigit( c ) || c == ',' ).ToArray() );

输出:512年、3159年

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

您可以仅使用Substring来删除view();:

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

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

:

Input =
  Input.Substring(5, Input.Length - 7)
  .Replace("'", String.Empty)
  .Replace("'"", String.Enmpty);
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);

希望对您有所帮助

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

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);

使用如下:

            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());
    var result = new string(Input.ToCharArray().
SkipWhile (i => i != '''').
TakeWhile (i => i != ')').ToArray());

为什么不使用正则表达式呢?正则表达式经过了大量优化,比任何手写的hack都要快得多。

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

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

在我的电脑上重复100万遍以上内容大约需要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*)?)"

更通用

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

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