471,317 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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
Jan 11 '06 #1
10 1419
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.
Daniel R. Rossnagel wrote:
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

Jan 11 '06 #2
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
"Daniel R. Rossnagel" <dr*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Jan 11 '06 #3
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...

"Daniel R. Rossnagel" <dr*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Jan 11 '06 #4
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
"Denis Dougall" <De***********@here.there.com> wrote in
news:er**************@tk2msftngp13.phx.gbl:
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
"Daniel R. Rossnagel" <dr*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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



Jan 11 '06 #5
Well then what about "reference Microsoft.VisualBasic.dll", and use
IsNumeric. What performance impact would that have?

Denis
"Gabriel Magana" <no***@nospam.com> wrote in message
news:uv**************@TK2MSFTNGP12.phx.gbl...
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...

"Daniel R. Rossnagel" <dr*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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


Jan 11 '06 #6
> 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.
Jan 11 '06 #7
Gabriel Magana wrote:
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.


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
sk***@pobox.com 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

Jan 11 '06 #8
Thanks, were of much utility
"John Murray" <jm*****@pluck.com> escribió en el mensaje
news:OU****************@TK2MSFTNGP15.phx.gbl...
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.
Daniel R. Rossnagel wrote:
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

Jan 11 '06 #9
Denis Dougall wrote:
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

Jan 11 '06 #10
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Denis Dougall wrote:
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

Jan 11 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Simon Wigzell | last post: by
3 posts views Thread by Mike Collins | last post: by
14 posts views Thread by Aman JIANG | last post: by
reply views Thread by rosydwin | last post: by

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.