The Mask
The Mask

Reputation: 17427

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 ");" 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! :)

Upvotes: 4

Views: 321

Answers (10)

Arjun Shetty
Arjun Shetty

Reputation: 1585

Hope this helps

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

Upvotes: 2

Dunes
Dunes

Reputation: 40713

Why don't you want to use regular expressions? Regular expressions are heavily optimised and will be much faster than any hand written hack.

This is java (as I run linux and can't run c# as a result), but I hope you get the idea.

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

A million repetitions of the above runs in about 6 seconds on my computer. Whereas, the regular expression below runs in about 2 seconds.

// 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*)?)"

Upvotes: 1

Brandon Moretz
Brandon Moretz

Reputation: 7621

You could use a simple linq statement:

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

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

Output: 512,3,159

If you want the spaces, just add a check in the where clause.

Upvotes: 4

Vlad Bezden
Vlad Bezden

Reputation: 89597

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

Upvotes: 1

Peter O.
Peter O.

Reputation: 32878

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

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283684

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

Upvotes: 2

gordy
gordy

Reputation: 9786

fastest way would be Input = Input.Substring(5, Input.Length - 7)

Upvotes: 0

agent-j
agent-j

Reputation: 27923

IndexOf, LastIndexOf, and Substring are probably fastest.

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

Upvotes: 1

Guffa
Guffa

Reputation: 700362

You could use just a Substring to remove the view( and );:

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

Upvotes: 2

Vlad Bezden
Vlad Bezden

Reputation: 89597

More generic

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

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

Upvotes: 0

Related Questions