September 05, 2007

Removing Non-Numeric Characters from a string (The easy way)

I was searching the web for a way to easily remove all non numeric characters from a string, but the only thing I found was some "for loop" examples, which really slowed down my application, so after a little research I came up with a simple way to do it using Regular Expressions

Here it goes:

string Temp = "Hax00r L33t";
string Output = Regex.Replace(Temp, "[^0-9]", "");


Back to Home

25 comments:

  1. Sheer genius!
    Sometimes the obvious ISN'T.

    Many thanks for this, you've saved me hours of silliness.

    ReplyDelete
  2. Good stuff, thanks.

    ReplyDelete
  3. thanks, that's gooode

    ReplyDelete
  4. Terribly elegant!

    ReplyDelete
  5. Doesn't work very well if you have a decimal point though does it? Needs a bit more work.

    ReplyDelete
  6. Apologies:

    Regex.Replace(Temp, "[^.0-9]", "") works. I was confused for a momemt thinking that the . stood for the wildcard (any) character and that I had to escape it.

    ReplyDelete
  7. Decimal point is a non-numeric character, now isn't it?

    ReplyDelete
  8. elegant, indeed. thanks!

    ReplyDelete
  9. Very nice - perfect solution

    ReplyDelete
  10. Kudos to you and Kudos to Isaac that published the version for floating point numbers.

    ReplyDelete
  11. What about negative numbers?
    Here is the full fledged version:

    Regex.Replace(Temp, "[^-.0-9]", "")

    Enjoy
    G~

    ReplyDelete
  12. I implemented two extension methods for string to remove non-numeric chars. One with a loop and one using the RegEx.

    The extension method using the loop is faster.

    ReplyDelete
  13. Tnx great solution!

    You and google rock!!!

    ReplyDelete
  14. Nice!

    Thanks for sharing!

    Sébastien D'Errico

    ReplyDelete
  15. sweet! Thanks.

    ReplyDelete
  16. Many thanks! you just saved me hours of messing about with something that should be obvious! :)

    ReplyDelete
  17. Very useful. Thanks!

    ReplyDelete
  18. Sheer beauty!

    ReplyDelete
  19. oh! this is cool dude :)
    simple'n useful

    ReplyDelete
  20. "the only thing I found was some 'for loop' examples, which really slowed down my application"

    ummmm, I think you'll find that regex also loops through your string and probably has much more overhead than a simple specifically written piece of code. It's selling point is ease of coding, not speed of execution.

    ReplyDelete
  21. the whole thing fails when the string as all characters and then a single number

    ReplyDelete
  22. Here is the Linq implementation, which, surprisingly, performs a bit faster than Regex:

    string temp = "h4xx0r 133t";
    string output = new string(temp.Where(ch => char.IsDigit(ch)).ToArray());

    ReplyDelete
  23. What function should we include to use regex.replace?

    ReplyDelete
  24. Nice one added this as an extension method see below:

    public static class StringExtensionMethods
    {
    public static string RemoveNonNumericCharacters(this string suppliedString)
    {
    return Regex.Replace(suppliedString, "[^0-9]", "");
    }
    }

    ReplyDelete

Note: Only a member of this blog may post a comment.