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 Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in parsing, regular expressions, strings and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Remove non-digits or non-letters from a string in C#

  1. Protiguous says:

    I think the examples might be switched..

    • RodStephens says:

      I think it’s correct. The caret ^ in the regular expression means “not” so, for example, [^0-9] means to match everything that is *not* a digit. It then replaces those characters with an empty string to remove everything that isn’t a digit.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.