BLOG.CSHARPHELPER.COM: Easily remove all non-digit or non-letter characters from a string in C#
Easily remove all non-digit or non-letter characters from a string in C#
Sometimes you might want to extract only the digits, letters, or some other group of characters from a string. You could loop through the string examining each character individually. Fortunately there's a much easier way to do this.
The regular expression class Regex provides a static Replace method that replaces characters matching a pattern with a new value. This example uses this method to replace non-digits with "" and to replace non-letters with "".
// Display only digits. txtDigits.Text = Regex.Replace(txtString.Text, "[^0-9]", ""); }
The key is the pattern used by Replace. For example, consider the first pattern [^a-zA-Z]. The brackets enclose a pattern giving a list of characters that the group could match. In this case, the pattern includes characters in the ranges a-z and A-Z. The ^ symbol at the beginning means "not" so this pattern matches any single character that is not in the range a-z or A-Z. In other words it matches non-letters. The final parameter replaces any matched character with "" so the result contains only letters.
The second call to Replace similarly removes all non-digits.
Comments