checking string if it is an integer

  • Thread starter Thread starter luke
  • Start date Start date
L

luke

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.
 
luke said:
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;
}
 
There may be a better way, but this should work:

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

Scott
 
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.
 
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..
 
luke said:
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.
 
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... ;-)
 
Back
Top