As I can determining if string can be turned to a numerico value?

  • Thread starter Thread starter Daniel R. Rossnagel
  • Start date Start date
D

Daniel R. Rossnagel

As I can determining if string can be turned to a numerico value?, since to
contain alfanumeric data it returns an error to me.
as I can avoid the following error?

String str="123";
int valInt = Convert.ToInt32(str); //OK
String str="StringXX"
int valInt = Convert.ToInt32(str); //ERROR
 
In the 2.0 framework, look for the TryParse method .... otherwise, the
easiest approach (not necessarily the most speedy) is to wrap the
convert in an exception handler.
 
Using Regular expressions is the most cost effective way.

private static Regex _isNumber = new Regex(@"^\d+$");

public static bool IsInteger(string theValue)
{
Match m = _isNumber.Match(theValue);
return m.Success;
} //IsInteger

OR
If you dont find that to your taste use this

public bool IsNumeric(string s)
{
try {
Int32.Parse(s);
}
catch {
return false;
}
return true;
}


HTH,

Denis
 
Just one note: Exceptions are very time consuming to handle, they should not
be a part of normal execution...

Not that I can claim to never have done it, but it really is not solid
design to allow exceptions to trigger as part of normal system function...
 
Regular expressions are certainly not the most cost effective. In fact
its just wrong - your regex doesn't account for integers that are too
large, nor does it account for negative numbers.

Under .NET 2.0, you can use int.TryParse(...). Under 1.1, the try/catch
with int.Parse is better than the regex method.

-mdb
 
Well then what about "reference Microsoft.VisualBasic.dll", and use
IsNumeric. What performance impact would that have?

Denis
 
Well then what about "reference Microsoft.VisualBasic.dll", and use
IsNumeric. What performance impact would that have?

None... That'd be a good thing to do.
 
Gabriel said:
None... That'd be a good thing to do.

Interesting that you think that would be a good thing to do, but using
exceptions would be too expensive.

I looked at this a while back - search for IsNumeric and
(e-mail address removed) on google groups and you'll find the code (I believe).
Here are the results I got (this was for integers, btw):

JustException: 00:01:15.7989936
HardCodedCheck: 00:00:00.7010080
DoubleTryParse: 00:00:40.8387232
Regex: 00:00:43.0418912
IsNumeric: 00:01:06.9062064

So using IsNumeric isn't really that much cheaper than using
exceptions. Note the raw speed of the hard-coded check though... one in
the eye for those who say that regular expressions are always the
fastest way to analyse text ;)

Jon
 
Thanks, were of much utility


John Murray said:
In the 2.0 framework, look for the TryParse method .... otherwise, the
easiest approach (not necessarily the most speedy) is to wrap the convert
in an exception handler.
 
Denis said:
Using Regular expressions is the most cost effective way.

What exactly due you mean by "cost effective" here? It certainly isn't
the cheapest way of working - it's fairly easy to write a hard-coded
check which is many times quicker than a regular expression.

Using a regular expression is more efficient than using a try/catch,
but arguably harder to read/debug. (It depends on your level of regex
ability.)

Jon
 
I tend to do most of my work in Perl, so regex and parsing is second nature,
the scripts are small and the speed of execution (of my scripts) is usually
across a network to end hosts, so that is usually the performance bottle
neck and not the code performance. The cost effective was just a flippant
remark and the code posted did not speak to the exact issue the poster
asked. I figured sample code or suggestions would be more appropriate then
giving the solution so the poster can use what works best for them or that
with which they are familiar. I will be more accurate in the future.



Thanks for the time trials that was interesting to see those results.



What I like about the discussion groups is that users of various levels can
get insight into different solutions and the background of those solutions.
I posted some solutions that were criticized and then posted the VB DLL as
that is where I first ran across IsNumeric.



Thanks for your input Jon!



Denis
 

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

Back
Top