473,396 Members | 1,936 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 string if it is an integer

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.
Nov 15 '05 #1
6 40395
"luke" <lu*****@yahoo.com> wrote in message
news:87*************************@posting.google.co m...
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.


try {
Int32.Parse(mystring);
return true;
}
catch (FormatException) {
return false;
}

--
Iain Simpson
Nov 15 '05 #2
There may be a better way, but this should work:

try
{
myInt = Int32.Parse(myString);
}
catch
{
//not an integer
}

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

Nov 15 '05 #3
try {
Int32.Parse(mystring);
return true;
}
catch (FormatException) {
return false;
}


catch (OverflowException) {
return false;
}

You'll want to do that too, incase the number is too big/small for an int to
hold.

--
Iain Simpson
Nov 15 '05 #4
luke <lu*****@yahoo.com> wrote:
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.


The simplest thing is to use Int32.Parse, returning true if it succeeds
and false if it throws an exception. Almost whatever you do, you'll
need to do that at some stage.

If, however, performance is important and you'll have a fair number of
strings which *aren't* ints, you might like to do some filtering first
to throw away strings which clearly aren't ints. I investigated this a
while ago and posted my results to one of the microsoft.* groups - a
groups.google.com search with me as the poster and IsNumeric in the
body should find the comparison (and fastest code) if you're interested
- let me know if you have problems finding it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
try {
Int32.Parse(mystring);
return true;
}
catch (FormatException) {
return false;
}


Argh. I need more coffee. Make sure that mystring isn't null either.
I suppose it might be easier to just catch Exception, but that might cause
undesired behaviour in exceptional ;) circumstances where the exception thrown
isn't due to the Parse failing..

--
Iain Simpson

Nov 15 '05 #6
I don't know C# well enough to know if there's an equivalent way to do
this (I just started following this newsgroup while at the same time
learning Java) but in Smalltalk I would add the following method to
CharacterArray (superclass of String):
isNumeric
"
return true if the string is a valid numeric expression:
'123' isNumeric - true
'123.45' isNumeric - true
'.45' isNumeric - true
'+123.45' isNumeric - true
'-123.45' isNumeric - true
'12.3.45' isNumeric - false
'123.45e100' isNumeric - true
'123.45e-100' isNumeric - true
'5.23e-6' isNumeric - true
'5.23e-6e' isNumeric - false

we use the current locale's definition of the decimal point character.
"

^self matchesRegex: (('[+-]?[0-9]*\' copyWith: Locale current
numberPolicy decimalPoint) , '?[0-9]+(e-?[0-9]+)?')

To avoid the whole "if it's a null" thing, I would add an isNumeric to
Object that simply returns false--so it wouldn't matter if it was a
String, a nil object, or an object of any other type--except perhaps a
subclass of Number--which should probably return "true", after all,
numbers are numeric... ;-)

luke wrote:
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.


--
..tom
remove email address' dashes for replies
opensource middleware at <http://isectd.sourceforge.net>
http://gagne.homedns.org
Nov 15 '05 #7

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

Similar topics

5
by: Tony Vasquez | last post by:
I want to turn the string "100,144" to numbers, to use for resizeTo(100,144) <--- here. Please advice, or post scriptlette. Thanks Tony Vasquez
2
by: Pierre | last post by:
I need to check the length of a string in a textbox control. I used RegularExpressionValidator with ".{0,20}" to check if the string is between 0 and 20 characters. Is it possible to use the other...
5
by: Joergen Bech | last post by:
Basically, I want to convert hex values in the range "00000000" to "FFFFFFFF" to a signed, 32-bit Integer value. In VB6, I could just write lngValue = Val(hexstring$). In VB.Net, I seem to be...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
7
by: darrel | last post by:
I have a function call that passes a value from a dataset field: myFunction(ds.Tables(0).Rows(0)("myField").ToString) The function then receives this: myFunction(ByVal myValue as string) ...
4
by: silversnake | last post by:
Hi , dose one know the code for checking through a string for upper or lower case char's and reverse them of find ? thanks
6
by: Jeff | last post by:
Could someone tell me the easiest way to check a string of variable length to see if it consists of all blank characters? ....or perhaps more generally, to see if all of the characters are the...
5
by: zivon | last post by:
Hello everyone ! I made a price calculator for a guest house. I have diffrent sale campaigns, all the time, I made a table with campaign name and discount, for example: name "pensioners"...
4
by: ziycon | last post by:
I have a string passed in and I want to check validate it to make sure that multiple values don't exist at the start, the below would only check the entire string for the value??...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.