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.8.0_1031]
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

    200910_Wales0071 2010_0110Wales0038 IMG_5481 2008_0505SevenSisters0025
  • Tags

  • RSStwitter logo

  • Comments