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?
CodePudding user response:
string result = string.Concat(s.Where(char.IsLetterOrDigit));
CodePudding user response:
You can use char.IsLetterOrDigit()
for that.
CodePudding user response:
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 ", "");