Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 403
The Mask Avatar asked Dec 10 '22 07:12

The Mask


1 Answers

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.

like image 112
Brandon Moretz Avatar answered Dec 17 '22 10:12

Brandon Moretz