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 "".

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 similarly removes all non-digits.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.