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?
You can use TryParse to determine if the string can be parsed into an integer.
The boolean will tell you if it worked or not.
I've used this function several times:
But you can also use;
From Benchmarking IsNumeric Options
alt text http://aspalliance.com/images/articleimages/80/Figure1.gif
alt text http://aspalliance.com/images/articleimages/80/Figure2.gif
In case you don't want to use int.Parse or double.Parse, you can roll your own with something like this:
Pull in a reference to Visual Basic in your project and use its Information.IsNumeric method such as shown below and be able to capture floats as well as integers unlike the answer above which only catches ints.
You can always use the built in TryParse methods for many datatypes to see if the string in question will pass.
Example.
Result would then = True
Result would then = False
You can also use:
It will return
true
for all Numeric Digits (notfloat
) andfalse
if input string is any sort of alphanumeric.Please note:
stringTest
should not be an empty string as this would pass the test of being numeric.