简体   繁体   English

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

[英]What's the best way to remove strings?

I need ideas with the best performance to remove/filter strings我需要具有最佳性能的想法来删除/过滤字符串

I have:我有:

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

What's the best performance way to remove "view(" and ");"删除“view(”和“);”的最佳性能方式是什么? and the quotes?和报价? I can do this:我可以做这个:

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

but it seems rather inelegant.但它似乎相当不雅。

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

it seems rather better似乎更好

I want no do it by using regular expression;我不想通过使用正则表达式来做到这一点; I need make the faster application what I can.我需要尽我所能做出更快的应用程序。 Thanks in advance: :)提前致谢: :)

You could use a simple linq statement:您可以使用简单的 linq 语句:

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

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

Output: 512,3,159 Output:512,3,159

If you want the spaces, just add a check in the where clause.如果你想要空格,只需在 where 子句中添加一个检查。

You could use just a Substring to remove the view( and );您可以只使用Substring来删除view(); :

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

Other than that it looks reasonably efficient.除此之外,它看起来相当有效。 Plain string operations are pretty well optimised.纯字符串操作得到了很好的优化。

So:所以:

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

Hope this helps希望这可以帮助

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

Use the following:使用以下内容:

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

IndexOf, LastIndexOf, and Substring are probably fastest. 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);
    var result = new string(Input.ToCharArray().
SkipWhile (i => i != '\'').
TakeWhile (i => i != ')').ToArray());

Why don't you want to use regular expressions?为什么不想使用正则表达式? Regular expressions are heavily optimised and will be much faster than any hand written hack.正则表达式经过大量优化,比任何手写的 hack 都要快得多。

This is java (as I run linux and can't run c# as a result), but I hope you get the idea.这是 java (当我运行 linux 并因此无法运行 c# 时),但我希望你明白。

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

A million repetitions of the above runs in about 6 seconds on my computer.在我的电脑上,上述一百万次重复运行大约需要 6 秒。 Whereas, the regular expression below runs in about 2 seconds.然而,下面的正则表达式运行大约 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);

If you want to find valid decimals as well as integers then use the following pattern instead.如果要查找有效的小数和整数,请改用以下模式。 Though it runs slightly slower than the original.虽然它的运行速度比原来的稍慢。

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

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

More generic更通用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM