Title: Remove non-digits or non-letters 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. The following code uses that method to replace non-digits and non-letters with "".
private void btnReplace_Click(object sender, EventArgs e)
{
// Display only letters.
txtLetters.Text =
Regex.Replace(txtString.Text, "[^a-zA-Z]", "");
// 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 uses the pattern [^0-9] to remove non-digits.
Download the example to experiment with it and to see additional details.
|