Thursday, February 9, 2012

C# strip string from special characters using Regex

Stripping strings from special characters is pretty easy.
If you want to add special characters to keep, add them in the Regex square brackets, i.e. [^a-zA-Z0-9_.-]
using System.Text.RegularExpressions;
string NoSpecialCharacters(string your_string)
{
    return Regex.Replace(your_string, "[^a-zA-Z0-9]+", "");
}

No comments:

Post a Comment