Search Chirayu Software Solutions Blog

Thursday, September 26, 2013

How to identify if a string is a number?

This will return true if input is all numbers. Don't know if it's any better than TryParse, but it will work.
Regex.IsMatch(input, @"^\d+$")
If you just want to know if it has one or more numbers mixed in with characters, leave off the ^ + and$.
Regex.IsMatch(input, @"\d")
Edit: Actually I think it is better than TryParse because a very long string could potentially overflow TryParse.
Post a Comment