473,395 Members | 1,468 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,395 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 1489
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simon Wigzell | last post by:
I am adapting a javascript pulldown menu system to my dynamic website generator - the arrays that hold the menu items information are read from a database and will be different for different users...
12
by: Jozef | last post by:
Hello, Is there an easy way to determine the highest point in an array that contains a value? I'm dimensioning an array to hold up to 255 items, but if it only contains three, I don't want to...
1
by: ABC | last post by:
How to convert a date string to datetime value with custom date format? e.g. Date String Date Format Result (DateTime value) "05/07/2004" "MM/dd/yyyy" May 7, 2004 "01062005" ...
1
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as...
3
by: Mike Collins | last post by:
I'm not feeling too smart right now, but I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. Can someone show...
3
by: BronxJedi | last post by:
I need help determining the correct syntax for achieving something: I have a property and in the set accessor I want the code to determine if the value is of a certain type. If it's not then I...
14
by: Aman JIANG | last post by:
hi i need a fast way to do lots of conversion that between string and numerical value(integer, float, double...), and boost::lexical_cast is useless, because it runs for a long time, (about 60...
6
by: sigkill9 | last post by:
I'm doing some reading in a python book and am doing one of the chapter exercises but I cant figure out how to get it to work and was hoping some of you python guru's could help out? Heres...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.