2

Removing non-alphanumeric characters from a string is simple work. For example:

StringBuilder sb = new StringBuilder();
foreach(var c in s)
{
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
        sb.Append(c);
}
return sb.ToString();

This method is suitable for ASCII characters.

Is there a solution for removing all non-alphanumeric characters in "UNICODE" texts?

3 Answers3

6
string result = string.Concat(s.Where(char.IsLetterOrDigit));
fubo
  • 44,811
  • 17
  • 103
  • 137
3

You can use char.IsLetterOrDigit() for that.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
0

Regular expression is an alternative; we replace all not unwanted letters (here we use \W+ patttern - one or more non alphanumeric characters) with empty string:

string result = Regex.Replace(s, @"\W+", "");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215