Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently using RegEx and C# to search a string and replace all non-numeric characters...

string strung = Regex.Replace(unstrung, @"[^\w]", "Strings");

What I am wondering is: Is this the fastest way to do this simple action? I have seen it quoted that RegEx can be slow and this seems a pretty simple thing.

Oddly I have a pretty ok knowledge of RegEx, but not one of the expansive .Net string tools etc...

Any help or direction pointing would be greatly appreciated.
Posted

It's certainly the fastest way to type it.

If you want to try to find faster ways, be a programmer and try different ways to approach the problem.

Performance for such a task would greatly depend on the length of the string involved.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-11 12:42pm    
That's true - my 5.
JasonD_S 20-Jan-11 3:35am    
I had almost forgotten about my asking this question. I have had a play around with this and although I haven't given it the time that proper experimentation requires. I think I've found a solution that works well for me. Which is basically leaving it how it is. The performance hasn't been an issue, although I thought it might and if it ain't broke don't fix it.

I guess my lesson learned here was; sometimes although pontificating and interest just aren't as important to the customer as a ticked requirement. Problem solved, next question. Although I might do some playing at home and write an article.
It depend on your alternative. If your task really needs replacements like this, you may want to do equivalent code using string operations yourself. Good chances are it will be slower than Regex. Are you curious enough to try?

Also, if you care about performance, it tells me you need to repeat this operation many times (otherwise, who cares about your performance). In this case you may gain performance if you use Regex constructor, not the static method you show. In this way regular expression can be pre-parsed -- just once.
 
Share this answer
 
Comments
JOAT-MON 11-Jan-11 13:00pm    
Insightful point +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900