Java check String is empty or blank or contains text using StringUtils Apache Commons Lang

Tags: Apache Commons Apache Commons Lang StringUtils String

Introduction

In this post we are going to learn how to use StringUtils utility class of Apache Commons Lang library in Java application to check if a string contains text, an empty string or a blank string.

Setup Apache Commons Lang in Java project

If you are using Gradle build then add the following dependency configuration into build.gradle file.

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'

Or add the following dependency XML tag to pom.xml file if you are using Maven build.

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

Or download commons-lang3-3.11.jar file from Apache Commons Lang download page at commons.apache.org

Check if a String is empty or null

Following Java code examples show you how to use StringUtils.isEmpty() method to check if an input string is empty or null value. And the StringUtils.isNotEmpty() method will return the opposite result.

Example Java code org.apache.commons.lang3.StringUtils.isEmpty()

import org.apache.commons.lang3.StringUtils;

public class StringUtilsIsEmptyExample {
    public static void main(String[] args) {
        String example1 = null;
        String example2 = "";
        String example3 = "          ";
        String example4 = "simplesolution.dev";

        boolean result1 = StringUtils.isEmpty(example1);
        boolean result2 = StringUtils.isEmpty(example2);
        boolean result3 = StringUtils.isEmpty(example3);
        boolean result4 = StringUtils.isEmpty(example4);

        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
    }
}
Output:

true
true
false
false

Example Java code org.apache.commons.lang3.StringUtils.isNotEmpty()

import org.apache.commons.lang3.StringUtils;

public class StringUtilsIsNotEmptyExample {
    public static void main(String[] args) {
        String example1 = null;
        String example2 = "";
        String example3 = "          ";
        String example4 = "simplesolution.dev";

        boolean result1 = StringUtils.isNotEmpty(example1);
        boolean result2 = StringUtils.isNotEmpty(example2);
        boolean result3 = StringUtils.isNotEmpty(example3);
        boolean result4 = StringUtils.isNotEmpty(example4);

        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
    }
}
Output:

false
false
true
true

Check if a String is empty, null or contains whitespace only

Following code examples will show you how to use StringUtils.isBlank() method to check if an input string is empty, null value or contains whitespace character only. The StringUtils.isNotBlank() method will return the opposite result.

Example Java code org.apache.commons.lang3.StringUtils.isBlank()

import org.apache.commons.lang3.StringUtils;

public class StringUtilsIsBlankExample {
    public static void main(String[] args) {
        String example1 = null;
        String example2 = "";
        String example3 = "          ";
        String example4 = "simplesolution.dev";

        boolean result1 = StringUtils.isBlank(example1);
        boolean result2 = StringUtils.isBlank(example2);
        boolean result3 = StringUtils.isBlank(example3);
        boolean result4 = StringUtils.isBlank(example4);

        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
    }
}
Output:

true
true
true
false

Example Java code org.apache.commons.lang3.StringUtils.isNotBlank()

import org.apache.commons.lang3.StringUtils;

public class StringUtilsIsNotBlankExample {
    public static void main(String[] args) {
        String example1 = null;
        String example2 = "";
        String example3 = "          ";
        String example4 = "simplesolution.dev";

        boolean result1 = StringUtils.isNotBlank(example1);
        boolean result2 = StringUtils.isNotBlank(example2);
        boolean result3 = StringUtils.isNotBlank(example3);
        boolean result4 = StringUtils.isNotBlank(example4);

        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
    }
}
Output:

false
false
false
true

Happy Coding 😊