Strip all punctuation from a string in VB.net

Cyclone picture Cyclone · Aug 31, 2009 · Viewed 11.4k times · Source

How do I strip all punctuation from a string in vb.net? I really do not want to do stringname.Replace("$", "") for every single bit of punctuation, though it would work.

How do i do this quickly and efficiently?

Other than coding something that codes this for me....

Answer

Guffa picture Guffa · Aug 31, 2009

You can use a regular expression to match anything that you want to remove:

str = Regex.Replace(str, "[^A-Za-z]+", String.Empty);

[^...] is a negative set that matches any character that is not in the set. You can just put any character there that you want to keep.