Posted By

Anthony on 04/06/09


Tagged

regex whitespace empty String null


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

ddevening
umang_nine
khouser


Extension method to test whether a string is null or empty or whitespace


 / Published in: C#
 

  1. public static bool IsNullOrEmpty(this string input)
  2. {
  3. if (string.IsNullOrEmpty(input))
  4. return true;
  5.  
  6. return string.IsNullOrEmpty(input.Trim());
  7. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: GothikX on April 23, 2009

Using Regex is probably overkill here; a more efficient alternative would simply be (on the last line): return string.IsNullOrEmpty(input.Trim()); although it includes more characters than the regex version.

Posted By: Anthony on January 6, 2010

Thanks GothikX. I always assumed that Trim only got rid of spaces, which is why I used the RegEx. Your comment caused me to actually look at the docs on MSDN. Looks like Microsoft has updated it a little since VB5. :-) Thanks again.

You need to login to post a comment.