Verify Integer Value

  • Thread starter Thread starter David Krussow
  • Start date Start date
D

David Krussow

I need to test to see if a string value can be converted to an integer. I
thought I'd try Convert.ToInt32(strTemp) within a Try Catch block. Is there
a better way to accomplish this?

Thanks.
 
I don't know if it more optimized but you could go through every char in the
string and validate it, it is probably not worth the effort :)

- Robin
 
David said:
I need to test to see if a string value can be converted to an integer. I
thought I'd try Convert.ToInt32(strTemp) within a Try Catch block. Is there
a better way to accomplish this?

Thanks.

Currently, that is about the best way, although you run into needless
exceptions. For some reason, the .Net framework designers didn't think that it
was worthwhile to implement a parser/converter that does *not* rely on
exceptions to indicate status.

The double type provides a TryParse method that will attempt the
parse/conversion, and returns a true/false status. It has been mentioned that
the next release of the framework will include TryParse methods on the other
numeric types.
 
Greets,

There are a couple of ways to do this. The first is by using the
Convert.ToInt32() method as you pointed out. There is also the Parse()
method of the Int32 class which can be used to accomplish the same thing.
In either case, you will need to check for an exception if it cannot be
parsed.

Another alternative is to use the RegEx class in the
System.Text.RegularExpressions namespace in order to match against a regex
pattern. Below is an example which uses RegEx to perform the validation:

// required using for RegEx class
using System.Text.RegularExpressions;

public bool IsInteger(string data)
{
return Regex.IsMatch(test, @"^[-+]?[0-9]\d*\.?[0]*$");
}

A good link which has a library of regular expressions patterns which
can be used with the RegEx class is the regex library:
http://www.regexlib.com/

They have several useful patterns which can be used to validate e-mail,
dates, time, numbers, etc.

Regards,

Joe
 
Unfortunately, to make something globalized, you'll need to take into
account each locale's parsing rules :(. So unless you are a master of these
things and know every possible way to write a number in every locale, you
still have to rely on Int32.Parse().
-mike
MVP

Joe Delekto said:
Greets,

There are a couple of ways to do this. The first is by using the
Convert.ToInt32() method as you pointed out. There is also the Parse()
method of the Int32 class which can be used to accomplish the same thing.
In either case, you will need to check for an exception if it cannot be
parsed.

Another alternative is to use the RegEx class in the
System.Text.RegularExpressions namespace in order to match against a regex
pattern. Below is an example which uses RegEx to perform the validation:

// required using for RegEx class
using System.Text.RegularExpressions;

public bool IsInteger(string data)
{
return Regex.IsMatch(test, @"^[-+]?[0-9]\d*\.?[0]*$");
}

A good link which has a library of regular expressions patterns which
can be used with the RegEx class is the regex library:
http://www.regexlib.com/

They have several useful patterns which can be used to validate e-mail,
dates, time, numbers, etc.

Regards,

Joe

David Krussow said:
I need to test to see if a string value can be converted to an integer. I
thought I'd try Convert.ToInt32(strTemp) within a Try Catch block. Is there
a better way to accomplish this?

Thanks.
 
Actually, for speed-critical things, there's quite a bit of speed to be
gained. But, then you run into the culture issues if you do your own
validation.
-mike
MVP
 
Michael Giagnocavo said:
Actually, for speed-critical things, there's quite a bit of speed to be
gained. But, then you run into the culture issues if you do your own
validation.
-mike
MVP

Which is why I'd like to know there isn't TryParse on the other (non-double)
primitive numeric types...

We are on the second public rev of .Net, and it is still incomplete at the
core. Support for screen scaping is there, though...
 
Julie said:
Which is why I'd like to know there isn't TryParse on the other (non-double)
primitive numeric types...

We are on the second public rev of .Net, and it is still incomplete at the
core. Support for screen scaping is there, though...

Well, we're only *just* on the second public rev - 1.0->1.1 was more
for bug fixes than new features, for the most part. I agree that
Int32.TryParse is annoyingly missing, but it *will* be in Whidbey from
all I've seen.
 
Since a string comprises of alphanumeric characters, to expect an
inbuilt function to do the validation may be asking for too much.
Nevertheless, the string class is basically a collection of characters,
you could write your own class that scans the whole string and find if
any non-numeric characters are present.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top