473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Checking if a string is numeric...

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)..

Jul 26 '07 #1
9 7297
"moo" <nt*****@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
Anyone know?
http://msdn2.microsoft.com/en-us/lib....tryparse.aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 26 '07 #2
moo wrote:
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
Jul 26 '07 #3
On Thu, 26 Jul 2007 15:41:50 -0700, moo <nt*****@gmail.comwrote:
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
Jul 26 '07 #4
moo <nt*****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 26 '07 #5
moo
On Jul 26, 3:49 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
moo wrote:
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
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?

Jul 26 '07 #6
moo <nt*****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 26 '07 #7
"moo" <nt*****@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
Bah, TryParse sucks terribly since you have to pass in a reference!
What a strange statement! Care to elaborate...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 26 '07 #8
moo wrote:
On Jul 26, 3:49 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
>moo wrote:
>>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.

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
Jul 27 '07 #9
Peter Duniho wrote:
On Thu, 26 Jul 2007 15:41:50 -0700, moo <nt*****@gmail.comwrote:
>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.
Python isdigit is for string not for char.

In fact Python does not have a char type.

Arne

Jul 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: luke | last post by:
Hello all, I need to check if the content in a string is an int and return a boolean. I am sure there is a way to do it, but I just dont know. Please help. Thanks, Luke.
4
by: Darin Browne | last post by:
Is there anything built into .NET Framework I could use to check if a string only has numerics and alphanumerics in it? If anything else but a numeric or alpha has been entered I want it to...
2
by: Ant | last post by:
Hi, I'm wondering how you can check a string that supposed to represent a numeric value. e.g. in old VB6 there was a method IsNumeric(). Is there an equivelant in C#? Many thanks for...
8
by: Brendan | last post by:
There must be an easy way to do this: For classes that contain very simple data tables, I like to do something like this: class Things(Object): def __init__(self, x, y, z): #assert that x,...
5
by: | last post by:
Hello, Is there a command in C# which will allow me to check whether or not a variable is a number? Something similar to the VB isNumeric command? Thanks, Ben
7
by: bcpkh | last post by:
Hello All I need to check a string to make sure it does not contain any non numeric characters, the problem that I face is that the string is fairly long, 2784601121574585949, strtol etc. can't...
15
by: H. | last post by:
What's the easiest way to check if an argument entered at the command line is a number? I know that one way would be to treat the argument as a character string, and then manually check each...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
7
by: Jim | last post by:
Hi guys, The variable '$pixels' in the code below may be an integer type or an integer represented as a string type. I need to check if the value is indeed one of these. Is the following code...
7
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.