Pierre Greborio.NET

Talking about .NET world

IsNumeric

To test if the string content is a valid integer we can use the VB function IsNumeric. C# doesn't provides such a function then we have to write our own helper function to do so. One of the most common approach I've seen on the newsgroups is the following code:

public static bool IsNumeric(string inputData)
{
 try
 {
  int.Parse(inputData);
  return true;
 }
 catch
 {
  return false;
 }
}

If the string isn't a valid integer I'll get a FormatException or OverflowException if it is valid but out of the integer domain. Another approach is to use Convert.ToInt32() instead of Parse. From a performance point of view the two solutions are pretty the same.

A very competitive way is to use Regular Expressions checking if the string data contains really only digits:

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

public static bool IsNumeric(string inputData)
{
 Match m = _isNumber.Match(inputData);
 return m.Success;
}

In this case, the regular expression is compiled once and then the solution is, more or less, 4 times faster than the previous ones.

Comments

Colt said:

Hi Pierre,

Just FYI: Ambrose just published an article in this topic:
http://aspalliance.com/Ambrose/Articles/IsNumeric.aspx

# July 20, 2003 10:54 PM

Frans Bouma said:

You can also use Double.TryParse. THen you don't have to come up with the try/catch yourself
# July 20, 2003 11:03 PM

Martin Spedding said:

The problem I found with using the try catch approach is that if you don't trim the value first it might contain a carriage return character. If it does then to the user it looks like the have typed in a number but that get back the message that it is not numeric.

Martin Spedding
# July 20, 2003 11:43 PM

SBC said:

In the regexp - would it account for hex strings?
# July 21, 2003 12:05 AM

Pierre Greborio said:

inputData expects a string containing a single word (then no trims or other string manipulation).

The Regular expression I wrote handle only digits.

Thanks to all for your comments
# July 21, 2003 12:54 AM

Lorenzo Barbieri said:

Why don't you use directly the VB.NET version...
It's part of the framework... perhaps only in the WRONG assembly...
And AFAIK it's written in C#...
# July 21, 2003 1:29 AM

Pierre Greborio said:

This is another solution but I don't think that VB.NET function is so efficient as a C# implementation.
# July 21, 2003 2:02 AM

Lawrence Oluyede said:

Is there a way to use this function (the VB.NET builtin I mean) from C#? And does it worth it...or instead Pierre's solution is better...?
# July 21, 2003 9:05 AM

Chad Osgood said:

I actually wrote about the exact same thing discussed here, including the benchmarks posted by Ambrose in my user group's forums. You can see the benchmark I used here.
It seems rather odd that I posted that on the 11th, and there's an article by Ambrose on the same day using the same tests and the same test counts, etc.

Coincidence?
# July 21, 2003 12:04 PM

Pierre Greborio said:

Hello Chad,
yes this is a coincidence due to the comon problem for a C# developer. Maybe this is just the entropy of the subject. Anyway, thank for giving us the link of you article.
# July 24, 2003 4:56 AM