23 Sep 2009

Empty or null String in Java

Posted by gavin

It’s such a common bit of code that crops up everywhere. That familiar line:

if(var != null && var.length() > 0)

There are slight variations. Some use

if(var != null && !var.equals(""))

They’re all there to achieve the same purpose: check whether a String is null or empty.

I’ve always thought “there really should be a utility for this”, but written the code anyway. Today, when faced with repeating this pattern about 10 times, I finally decided to go and check the Apache Commons Lang javadoc and see if there was such a method. Unsurprisingly, there is, in StringUtils.

There’s actually a few:

  • isBlank(String)
  • isEmpty(String)
  • isNotBlank(String)
  • isNotEmpty(String)

Very handy.

VN:F [1.9.6_1107]
Rating: 0.0/10 (0 votes cast)
  • Print
  • Add to favorites
  • Facebook
  • StumbleUpon
  • del.icio.us
  • FriendFeed
  • Twitter
  • Digg

Leave a Reply

Message:

  • Browse

    or
  • Latest Content

  • Gallery

    IMG_5471 IMG_6750 img_5608 IMG_7162
  • Tags

  • RSS<img src="http://www.gavinwillingham.com/wp-content/plugins/twitter-widget-pro/twitter_logo_s.png" height="21" width="92" alt="twitter logo" />

  • Comments