Java – Check Empty String with Apache Commons

Apache Commons IO provides the StringUtils Class for checking empty string in Java.

Add the Apache Commons Lang in the pom.xml.

...
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.0.1</version>
</dependency>
...

 

Try the following Class.

import org.apache.commons.lang3.StringUtils;

public class CheckEmptyString {
  public static void main(String[] args) {
    String testString = null;
		
    if (StringUtils.isEmpty(testString)) {
      System.out.println("This string is empty.");
    }
  }
}

 

Done =)

5 thoughts on “Java – Check Empty String with Apache Commons”

    1. Yes, but that isEmpty() method of a string object provided by jdk6 will throw NullPointerException if the string is null.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.