If I have these strings:
"abc"
=false
"123"
=true
"ab2"
=false
Is there a command, like IsNumeric or something else, that can identify if a string is a valid number?
If I have these strings:
"abc"
= false
"123"
= true
"ab2"
= false
Is there a command, like IsNumeric or something else, that can identify if a string is a valid number?
UPDATE of Kunal Noel Answer
But, for this case we have that empty strings will pass that test, so, you can:
I know this is an old thread, but none of the answers really did it for me - either inefficient, or not encapsulated for easy reuse. I also wanted to ensure it returned false if the string was empty or null. TryParse returns true in this case (an empty string does not cause an error when parsing as a number). So, here's my string extension method:
Simple to use:
Or, if you want to test other types of number, you can specify the 'style'. So, to convert a number with an Exponent, you could use:
Or to test a potential Hex string, you could use:
The optional 'culture' parameter can be used in much the same way.
It is limited by not being able to convert strings that are too big to be contained in a double, but that is a limited requirement and I think if you are working with numbers larger than this, then you'll probably need additional specialised number handling functions anyway.
This will return true if
input
is all numbers. Don't know if it's any better thanTryParse
, but it will work.If you just want to know if it has one or more numbers mixed in with characters, leave off the
^
+
and$
.Edit: Actually I think it is better than TryParse because a very long string could potentially overflow TryParse.
With c# 7 it you can inline the out variable:
If you want to catch a broader spectrum of numbers, à la PHP's is_numeric, you can use the following:
Unit Test:
Keep in mind that just because a value is numeric doesn't mean it can be converted to a numeric type. For example,
"999999999999999999999999999999.9999999999"
is a perfeclty valid numeric value, but it won't fit into a .NET numeric type (not one defined in the standard library, that is).I guess this answer will just be lost in between all the other ones, but anyway, here goes.
I ended up on this question via Google because I wanted to check if a
string
wasnumeric
so that I could just usedouble.Parse("123")
instead of theTryParse()
method.Why? Because it's annoying to have to declare a
out
variable and check the result ofTryParse()
before you now if the parse failed or not. I want to use theternary operator
to check if thestring
isnumerical
and then just parse it in the first ternary expression or provide a default value in the second ternary expression.Like this:
It's just a lot cleaner than:
I made a couple
extension methods
for these cases:Extension method one
Example:
Because
IsParseableAs()
tries to parse the string as the appropriate type instead of just checking if the string is "numeric" it should be pretty safe. And you can even use it for non numeric types that has aTryParse()
method, likeDateTime
.The method uses reflection and you end up calling the
TryParse()
method twice which of course isn't as efficient, but not everything has to be fully optimized, sometimes convenience is just more important.This method can also be used to easily parse a list of numeric strings into a list of
double
or some other type with a default value without having to catch any exceptions:Extension method two
This extension method lets you parse a
string
as anytype
that has aTryParse()
method and it also lets you specify a default value to return if the conversion fails.This is better than using the ternary operator with the extension method above as it only does the conversion once, still uses reflection though...
Examples:
Outputs: