Is there any IsNumeric in C#?

  • Thread starter Thread starter Steve Kershaw
  • Start date Start date
S

Steve Kershaw

Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(string)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve
 
Import Microsoft.VisualBasic and use it!

if (Microsoft.VisualBasic.Information.IsNumeric("5"))
{
//Do Something
}

if (Microsoft.VisualBasic.Information.IsNumeric("yadda"))
{
//Do Something
}



Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
Steve,

Try this:

public static bool IsInteger(string theValue)
{
try
{
Convert.ToInt32(theValue);
return true;
}
catch
{
return false;
}
}
}
 
From Scott Hanselman's blog at :

http://www.hanselman.com/blog/ExploringIsNumericForC.aspx

Actually, this doesn't really do what IsNumeric does,
as IsNumeric should also return true for floating point numbers.

That code does not return true for floating point numbers,
which -nevertheless- *are* numbers.

That is really more of an "IsInt".

Importing Microsoft.VisualBasic does the complete job.




Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
In the .Net 2.0 CLR, there is an Int32.TryParse() method that doesn't throw
an exception, and you can use that. In .Net 1.1, you're pretty much stuck
with the alternatives that the others have already proposed.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
There is no equivalent for this function in C#. VB.NET has lots of
functions that C# developers have to create manually.
Or, you can import the visual basic namespace and use its IsNumeric function
from within C#.
 
Back
Top