free geoip Test if a String is Numeric - protected virtual void jaysonBlog
in

jaysonKnight.com

Welcome to my corner of the internet

protected virtual void jaysonBlog

A conduit to the voices in 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.

Share this post: Submit Test+if+a+String+is+Numeric to Technorati | Submit Test+if+a+String+is+Numeric to del.icio.us | Submit Test+if+a+String+is+Numeric to digg.com | Submit Test+if+a+String+is+Numeric to reddit.com | Submit Test+if+a+String+is+Numeric to DotNetKicks | Add Test+if+a+String+is+Numeric to Live Bookmarks
Only published comments... Aug 10 2005, 01:15 AM by Jayson Knight

Comments

 

Haacked said:

Since your trackbacks are down, I thought I'd do a manual trackback here. http://haacked.com/archive/2005/08/10/9293.aspx
August 11, 2005 1:58 AM
 

Keyvan Nayyeri said:

You added a false "q" to ASP.NET forums link! ;) And another issue about CS: Your commenting system does not remember me!!
August 11, 2005 10:47 AM
 

mousse said:

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..
August 11, 2005 8:01 PM
 

Jayson Knight said:

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!
August 11, 2005 11:30 PM
 

mousse said:

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
August 12, 2005 1:54 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About Jayson Knight

Jayson Knight was clueless to the computer programming world until he took a C++ class in college. The rest is proverbial history. He has been building applications targeting the .Net framework for 7 years, focusing mainly on internet technologies and database driven web application development.

Most recently he left the world of Corporate IT to finish up his degree in Chemistry, with an eye on Medical School and an Anesthesiology residency program. Read this post for more information.

He is also a Community Server MVP: Community Server is the software that runs this site, plus many others on the web. For more information, check out http://csmvps.com.

When he finds time to pry himself away from his computer and university studies, he can be found on the mountain bike trails when it's warm, and on the ski slopes when it's cold.

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

Terms of Service/Privacy Policy