How do I remove all non alphanumeric characters from a string except dash?

Luke101 picture Luke101 · Jul 9, 2010 · Viewed 379.6k times · Source

How do I remove all non alphanumeric characters from a string except dash and space characters?

Answer

Amarghosh picture Amarghosh · Jul 9, 2010

Replace [^a-zA-Z0-9 -] with an empty string.

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");