Checking if a string is numeric...

M

moo

Once again, an elegant way of doing it, and I'm sure there's a built-
in library function for it. In Python, Strings have a isdigit()
property that can be used to check if a string contains only digits,
which while I haven't looked at the code I imagine checks if each
character falls within the ascii character range of 0-9, etc.

I could write my own, but I'm sure there's a similar, easy way of
doing it in .Net. Anyone know? Double.isNaN() is out because I only
want integers, and although I can use Convert.ToInt32(), exception
handling seems like a pretty crappy way of doing a conditional (I'm
doing validation)..
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

moo said:
Once again, an elegant way of doing it, and I'm sure there's a built-
in library function for it. In Python, Strings have a isdigit()
property that can be used to check if a string contains only digits,
which while I haven't looked at the code I imagine checks if each
character falls within the ascii character range of 0-9, etc.

I could write my own, but I'm sure there's a similar, easy way of
doing it in .Net. Anyone know? Double.isNaN() is out because I only
want integers, and although I can use Convert.ToInt32(), exception
handling seems like a pretty crappy way of doing a conditional (I'm
doing validation)..

int.TryParse
a for loop and Char.IsDIgit
Regex.IsMatch

take your pick.

Arne
 
P

Peter Duniho

Once again, an elegant way of doing it, and I'm sure there's a built-
in library function for it. In Python, Strings have a isdigit()
property that can be used to check if a string contains only digits,
which while I haven't looked at the code I imagine checks if each
character falls within the ascii character range of 0-9, etc.

I don't know Python, but I'd guess the isdigit() function is similar to
Char.IsDigit(), which checks a specific character instance for being a
digit. You can of course apply the test to a string of characters to test
if they are all digits, but of course that's not a 100% perfect way of
knowing whether a string is parseable as an integer, since there are
strings that are all digits, but which are too long to be stored in an int.
I could write my own, but I'm sure there's a similar, easy way of
doing it in .Net. Anyone know? Double.isNaN() is out because I only
want integers, and although I can use Convert.ToInt32(), exception
handling seems like a pretty crappy way of doing a conditional (I'm
doing validation)..

For every Parse, there's a TryParse.

Okay, actually I'm not 100% sure about that. But for sure, there's an
int.TryParse(). It returns a true/false value depending on whether the
string could in fact be parsed as an integer.

Pete
 
J

Jon Skeet [C# MVP]

moo said:
Once again, an elegant way of doing it, and I'm sure there's a built-
in library function for it. In Python, Strings have a isdigit()
property that can be used to check if a string contains only digits,
which while I haven't looked at the code I imagine checks if each
character falls within the ascii character range of 0-9, etc.

I could write my own, but I'm sure there's a similar, easy way of
doing it in .Net. Anyone know? Double.isNaN() is out because I only
want integers, and although I can use Convert.ToInt32(), exception
handling seems like a pretty crappy way of doing a conditional (I'm
doing validation)..

If you're using .NET 2.0, int.TryParse or long.TryParse are your
friends. Note that they will return false if you give them a number
which is too big to handle - for instance, if you pass in
"999999999999999999999999999" that's too big for either of them despite
it only containing digits. A simple method to encapsulate the logic (a
foreach loop) would be the best solution if you wanted to handle that
case.
 
M

moo

int.TryParse
a for loop and Char.IsDIgit
Regex.IsMatch

take your pick.

Arne

Bah, TryParse sucks terribly since you have to pass in a reference!
The for-loop is basically what I was talking about, and if that and
the RegEx are the only options than I guess I'll just do
those...anyone know which one has better performance?
 
J

Jon Skeet [C# MVP]

moo said:
Bah, TryParse sucks terribly since you have to pass in a reference!

Well, you have to pass a parameter *by* reference (as an out parameter)
which isn't quite the same thing.

However, I'm interested to know in what way it "sucks terribly". It's
not exactly hard to supply an out parameter.
The for-loop is basically what I was talking about, and if that and
the RegEx are the only options than I guess I'll just do
those...anyone know which one has better performance?

A for or foreach loop will perform better than a regex in this case.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

moo said:
Bah, TryParse sucks terribly since you have to pass in a reference!

That is more or less given by its functionality.
The for-loop is basically what I was talking about, and if that and
the RegEx are the only options than I guess I'll just do
those...anyone know which one has better performance?

The for loop is much faster. Not as in 10% faster but
as in x10 faster.

Arne
 
G

Guest

Peter said:
I don't know Python, but I'd guess the isdigit() function is similar to
Char.IsDigit(), which checks a specific character instance for being a
digit. You can of course apply the test to a string of characters to
test if they are all digits, but of course that's not a 100% perfect way
of knowing whether a string is parseable as an integer, since there are
strings that are all digits, but which are too long to be stored in an int.

Python isdigit is for string not for char.

In fact Python does not have a char type.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top