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?
Hope this helps
Update As of C# 7:
The var s can be replaced by their respective types!
This is probably the best option in C#.
If you want to know if the string contains a whole number (integer):
The TryParse method will try to convert the string to a number (integer) and if it succeeds it will return true and place the corresponding number in myInt. If it can't, it returns false.
Solutions using the
int.Parse(someString)
alternative shown in other responses works, but it is much slower because throwing exceptions is very expensive.TryParse(...)
was added to the C# language in version 2, and until then you didn't have a choice. Now you do: you should therefore avoid theParse()
alternative.If you want to accept decimal numbers, the decimal class also has a
.TryParse(...)
method. Replace int with decimal in the above discussion, and the same principles apply.