![]() |
Is there any IsNumeric in C#?
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 |
Re: Is there any IsNumeric in C#?
Import Microsoft.VisualBasic and use it!
if (Microsoft.VisualBasic.Information.IsNumeric("5")) { //Do Something } if (Microsoft.VisualBasic.Information.IsNumeric("yadd a")) { //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 Kershaw" <steve_kershaw@yahoo.com> wrote in message news:1131048814.240063.207290@g43g2000cwa.googlegr oups.com... > 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 > |
RE: Is there any IsNumeric in C#?
Steve,
Try this: public static bool IsInteger(string theValue) { try { Convert.ToInt32(theValue); return true; } catch { return false; } } } "Steve Kershaw" wrote: > 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 > > |
Re: Is there any IsNumeric in C#?
From Scott Hanselman's blog at :
http://www.hanselman.com/blog/Explor...mericForC.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/ ====================================== "Dave" <Dave@discussions.microsoft.com> wrote in message news:D7AA7B02-7A4B-4C32-A36B-A63ED0BDB145@microsoft.com... > Steve, > > Try this: > > public static bool IsInteger(string theValue) > { > try > { > Convert.ToInt32(theValue); > return true; > } > catch > { > return false; > } > } > } > "Steve Kershaw" wrote: > >> 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 >> >> |
Re: Is there any IsNumeric in C#?
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. "Steve Kershaw" <steve_kershaw@yahoo.com> wrote in message news:1131048814.240063.207290@g43g2000cwa.googlegr oups.com... > 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 > |
Re: Is there any IsNumeric in C#?
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#. -- I hope this helps, Steve C. Orr, MCSD, MVP http://SteveOrr.net "Steve Kershaw" <steve_kershaw@yahoo.com> wrote in message news:1131048814.240063.207290@g43g2000cwa.googlegr oups.com... > 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 > |
Hey Guysw Check it out
Here m posting you IsNumeric function,tried too hard but got success at the end...:driver:
public static bool IsNumeric(object numberString) { char[] ca = numberString.ToString().ToCharArray(); for (int i = 0; i < ca.Length; i++) { if (!char.IsNumber(ca[i])) if (ca[i] != '.') return false; } if (numberString.ToString().Trim() == "") return false; return true; } |
Yes, there is no equivilant function in C#, but there is a very nice blog on this subject.
Look at dotnet.org.za/deonvs/archive/2004/07/06/2579.aspx Very helpfull info on this. I cannot link in any links, so can you have to copy the link |
hi,
there is a tryparse for double, so if you use that, choose the "NumberStyles.Integer" option and check that the resulting double is within the boundaries of Int32, you can determine if you string is an integer without throwing an exception. hope this helps private bool TryIntParse(string txt) { try { double dblOut = 0; if (double.TryParse(txt, System.Globalization.NumberStyles.Integer , System.Globalization.CultureInfo.CurrentCulture, out dblOut)) { // determined its an int, now check if its within the Int32 max min return dblOut > Int32.MinValue && dblOut < Int32.MaxValue; } else { return false; } } catch(Exception ex) { throw ex; } } |
Basically same as above but much simpler:
Code:
public static bool IsNumeric(object o) |
| All times are GMT. The time now is 10:21 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.