free geoip Test if a String is Numeric - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
A conduit to the voices inside my head.
Test if a String is Numeric

I came across this little dandy whilst browsing a very popular asp.net community site (code like this makes me want to beat whoever wrote it to death with a wet noodle); the question was “how do I test a string to see if it’s numeric”…one of the answers given was the following:

  
public bool IsNumeric(string s)
{
   try
   {
      Int32.Parse(s);
   }
   catch
   {
      return false;
   }
      return true;
} 

What…no finally block?  Just kidding.  Ahh, the proliferation of idiocy to up and coming developers at its finest.

Sidenote:  To anyone who doesn’t understand why the above code is oh so very evil, try/catch blocks should never ever be used to dictate control of flow or govern what a method returns.  Also, never catch an exception unless you are going to do something with it (repeat after me, thou shalt not swallow exceptions).  Not to mention the inherent processing overhead that the framework has to go through to catch such a worthless exception in this case (imagine if this block was called hundreds or thousands of times per second…ouch).  Resource-wise, generating exceptions are way up there. 

I personally would use double.TryParse() (and downcast accordingly depending on the result) at the very least.  At the very most I’d break the string down to a char array, and walk the array calling one of the (very) useful static char.Is<whatever> methods…first non<whatever> value, break out of the loop and return false.  I’ve posted before about the speed at which the framework can process char data…it’s very fast and effecient. 

The problem with solving something quick and dirty is that even after the quick, it’s still dirty.


Posted Wed, Aug 10 2005 1:15 AM by Jayson Knight
comments powered by Disqus

Comments

Haacked wrote re: Test if a String is Numeric
on Thu, Aug 11 2005 1:58 AM
Since your trackbacks are down, I thought I'd do a manual trackback here. http://haacked.com/archive/2005/08/10/9293.aspx
Keyvan Nayyeri wrote re: Test if a String is Numeric
on Thu, Aug 11 2005 10:47 AM
You added a false "q" to ASP.NET forums link! ;) And another issue about CS: Your commenting system does not remember me!!
mousse wrote re: Test if a String is Numeric
on Thu, Aug 11 2005 8:01 PM
er, an iterated char-array solution? sounds like a PITA for just assessing if a string's numeric.. what about regular expressions? i don't honestly know how weighty their overhead..
Jayson Knight wrote re: Test if a String is Numeric
on Thu, Aug 11 2005 11:30 PM
Someone provide the regex and I'll be happy to run some performance tests...I'm gunning for the char[] solution to be the fastest, but I'd love to be proven wrong!
mousse wrote re: Test if a String is Numeric
on Fri, Aug 12 2005 1:54 AM
ok, i made these without thinking too hard: for ints try ^([-+]?\d+)$ for floats, try ^([-+]?\d+\.?\d*)$ presuming +/- negatives rather than paren negatives. www.regexlib.com == da bomb

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy