C Sharp how to find if a given string is a number or not

Hi,

When using JavaScript many a times we have a requirement to find if a value is number or not. With JavaScript its pretty easy, as it gives us a function isNaN which tells us if the string is a number or not. But how can you tell in C# if a string is number or not?

When I confronted with the problem, one of the solution was to use the int.Parse() method inside a try catch block. Below is an example of what I meant

try

{ int.Parse("5000"); }

catch

{ }

If there is an exception that means the value is not a number otherwise the value is a number. The only problem with this method is the use of try catch block. It’s never good to use try-catch to define the flow of the system. I thought a bit and found another way around for this. Here is what I did.

string numberString = "00";

bool isnumber = true;

for (int i = 0; i < numberString.Length; i++)

{

    if (!Char.IsNumber(numberString[i]))

    {
          isnumber = false;

         // If this place is arrived the string is not a number
     }

}

Here I am looping through all the character inside the string and checking if they are number or not. If any one of them is not a number that the string cannot be a proper integer. And if all of them satisfy the condition the string a proper integer.

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 1/29/2008 7:29:27 PM

You realize there's a built in method called TryParse that is specifically for this?

string number = "00";

int tempInteger;
if(int.TryParse(number, out tempInteger))

Posted on 1/30/2008 12:06:35 AM

Just use TryParse method. It returns true if the conversion was successful, false otherwise. No exception is thrown.

Posted on 1/30/2008 12:26:50 AM

The most efficient method to decide if a string is int is the Int32.TryParse method.

public static bool TryParse(
string s,
out int result
)

The 'result' parameter returns the converted number or 0 it the conversion failed. Also the method returns true on successful conversion and false when the conversion failed.

Posted on 1/30/2008 2:14:16 AM

But what if the string representation of the number contains a minus sign (-) ?? That's perfectly valid for a string representation of a signed int, yet your code will not see it as a number.

Posted on 1/30/2008 6:47:57 AM

I think int.TryParse() is a great method if you want to check if the string is a number or not.

string number = "3"

int outputNumber;
if(int.TryParse(number, out outputNumber)
//Do some stuff with outputNumber
else
//Not a number

Posted on 1/31/2008 2:16:25 AM

How about Int32.TryParse?

Posted on 1/31/2008 4:17:23 AM

Make sure to put a "break;" to exit the loop quickly. Good luck.

Posted on 1/31/2008 6:15:45 AM

There is always int.TryParse

string numberString = "100";
int value;
if(int.TryParse(numberString, out value)
{
//You have a valid int
}

The only other problem with the Char.IsNumber approach is that it will only work for positive integers. "0.0" will fail as will "-0.0". Not a problem if you just want to test for integral values.

If you want to test for real values as well then double.TryParse will do the trick. You can also pass in optional localization info.

Posted on 1/31/2008 2:27:48 PM

Use TryParse..

Posted on 1/31/2008 3:03:07 PM

The way that I know most people tend to use for this is to use the Integer.TryParse() method.

int value = 0;
Integer.TryParse(myString, out value);

Since the method is a bool it will return true if value is a valid int, false if it is not a valid int.

Posted on 1/31/2008 5:22:56 PM

int.TryParse ?

Posted on 1/31/2008 5:24:57 PM

Didn't mean to dogpile, but there were no other comments visible when I wrote mine.

Posted on 1/31/2008 5:28:36 PM

HI All,

Thanks for your comments. Yes I know about int.TryParse(). The only problem I had with that was that it internally uses try and catch block to find the if the number is an integer or not. Which actually brings the same problem.

But that is not a bad option. Also as many people pointed out, my function is only useful when working with positive integer number. Actually My requirement when creating the function was that only. But yes the function does has limitation.

Posted on 1/31/2008 5:35:28 PM

Hi Robert,

All my comment goes through moderation, and that is why it took some time. I was a bit busy today and that is wahy it took some time for all the comment to come.

Posted on 1/31/2008 5:38:16 PM

HI Sam,

Good pointer, The break should be there in the for loop when we get that the string is not a number. :-)

Posted on 2/6/2008 1:27:17 AM

Some where i found this:

public static bool IsNumeric(object Expression)
{
bool isNum;
double retNum;
isNum = Double.TryParse(Convert.ToString(Expression),
System.Globalization.NumberStyles.Any,
System.Globalization.NumberFormatInfo.InvariantInfo,
out retNum);
return isNum;
}

Posted on 2/6/2008 7:27:44 AM

Int32.TryParse doesn't use try/catch, it's the best and safest way to check it.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia