This is a really cheap question: IsNumeric?

M

Matthias S.

Hi,

I'm honestly sorry for asking such a dumb question, but I can't figure out how
to do it: How do I check whether a string is numeric. I remember something like
IsNumeric in earlier VB days. I had a look at the int.Parse() but if a string is
not numeric a FormatException is thrown.

Thanks in advance!
 
D

Dennis Myrén

For simple integer types like Int16, Int32 and Int64
you could use something like:
public static bool IsInteger ( string s )
{
for (int i = 0; i < s.Length; i ++)
if (! char.IsNumber(s ))
return false;
return true;

}

However, it could be extended to be more flexible than that, regarding
System.Globalization.NumberStyles and so on.

For floating points like Single and Double have a look at Double.TryParse.
 
M

Mark Rae

Thanks in advance!

There are several ways to achieve this:

1) You could use a RegularExpression - something like:

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

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

2) You could "roll your own" function(s) e.g.

public static bool IsDecimal(string theValue
{
try
{
Convert.ToDouble(theValue);
return true;
}
catch
{
return false;
}
}
public static bool IsInteger(string theValue)
{
try
{
Convert.ToInt32(theValue);
return true;
}
catch
{
return false;
}
}

3) You could add a reference to the Microsoft.VisualBasic.dll, and use
Microsoft.VisualBasic.Information.IsNumeric( ...)
 
J

Jon Skeet [C# MVP]

Dennis Myrén said:
For simple integer types like Int16, Int32 and Int64
you could use something like:
public static bool IsInteger ( string s )
{
for (int i = 0; i < s.Length; i ++)
if (! char.IsNumber(s ))
return false;
return true;

}

However, it could be extended to be more flexible than that, regarding
System.Globalization.NumberStyles and so on.

For floating points like Single and Double have a look at Double.TryParse..


Double.TryParse can be used with integers as well - use NumberStyles to
say what it should try to parse.

In the 2.0 framework, there is Int32.TryParse etc as well.
 
J

Jon Skeet [C# MVP]

Mark Rae said:
There are several ways to achieve this:

<snip>

All of those are more expensive in my experience than a simple test of
each character in the string, with some length checking etc if you want
to make sure it's in a certain range. The simple test should then be
followed by the exception test for invalid cases it doesn't catch. (You
need to be careful to make sure that it doesn't claim something is
invalid when it's valid, but claiming something is valid when it's not
just means it'll be caught by the "normal" parse, so the penalty is
time rather than correctness.)

In a test of this I did a while ago (for floating point rather than
integer) the results were:

Straight exceptions: 00:01:15.7989936
Hard coded routine: 00:00:00.7010080
Double.TryParse: 00:00:40.8387232
Regular expressions: 00:00:43.0418912
IsNumeric from VisualBasic.Information: 00:01:06.9062064

Search for "hard coded routine" and "straight exceptions" in
groups.google.com to find the code.
 
N

Nicholas Paldino [.NET/C# MVP]

Actually, all primitive types now have a static TryParse method on them.
This was a big request for .NET 2.0.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dennis Myrén said:
For simple integer types like Int16, Int32 and Int64
you could use something like:
public static bool IsInteger ( string s )
{
for (int i = 0; i < s.Length; i ++)
if (! char.IsNumber(s ))
return false;
return true;

}

However, it could be extended to be more flexible than that, regarding
System.Globalization.NumberStyles and so on.

For floating points like Single and Double have a look at Double.TryParse.


Double.TryParse can be used with integers as well - use NumberStyles to
say what it should try to parse.

In the 2.0 framework, there is Int32.TryParse etc as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top