473,786 Members | 2,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1517
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(strin g theValue)
{
Match m = _isNumber.Match (theValue);
return m.Success;
} //IsInteger

OR
If you dont find that to your taste use this

public bool IsNumeric(strin g s)
{
try {
Int32.Parse(s);
}
catch {
return false;
}
return true;
}
HTH,

Denis
"Daniel R. Rossnagel" <dr*********@ho tmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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*********@ho tmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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******** ******@tk2msftn gp13.phx.gbl:
Using Regular expressions is the most cost effective way.

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

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

OR
If you dont find that to your taste use this

public bool IsNumeric(strin g s)
{
try {
Int32.Parse(s);
}
catch {
return false;
}
return true;
}
HTH,

Denis
"Daniel R. Rossnagel" <dr*********@ho tmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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.Visua lBasic.dll", and use
IsNumeric. What performance impact would that have?

Denis
"Gabriel Magana" <no***@nospam.c om> wrote in message
news:uv******** ******@TK2MSFTN GP12.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*********@ho tmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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.Visua lBasic.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.Visua lBasic.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.798993 6
HardCodedCheck: 00:00:00.701008 0
DoubleTryParse: 00:00:40.838723 2
Regex: 00:00:43.041891 2
IsNumeric: 00:01:06.906206 4

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******** ********@TK2MSF TNGP15.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
26972
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 of my system. Unfortunately the javascript menu system requires that each menu cell's width be dimensioned. if I don't hard code a large enough value then long menu item names are trimmed, besides the fact that it then looks ugly for most menu...
12
1621
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 cycle through all 255 if that's possible. any ideas? Thanks!
1
3116
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" "ddMMyyyy" June 1, 2005 "09-07-05" "MM-dd-yy" Sept 7, 2005 Is there any functions to convert?
1
4607
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 "dd/MM/yyyy" input as "01082003" convert to date value as, 01 Aug 2003 Example, User Defined Date Format as "yyyy,dd,MM"
3
12488
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 me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following: //Code I use to populate the dropdown list. ddAssignedTo.DataValueField = "PersonnelID"; ddAssignedTo.DataTextField =...
3
1364
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 need it to throw an error and if it is then it should proceed as normal. I was trying to do it thusly: if (! value == typeof(XXXXX)) but the compiler doesn't like that so I'm assuming my syntax is wrong. Thank you in advance.
14
2172
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 times slower than corresponding C functions) it's too expensive for my program. is there any way( library?) to do this fast and safely, please ?
6
7000
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 description of the problem to be solved: "A positive whole number n > 2 is prime if no number between 2 and the square root of n (inclusive) evenly divides n. Write a program that accepts a value of n as input and determines if the value is prime. If n...
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.