Fundamentals 8 min read

Mastering Apache Commons Lang StringUtils: Empty, Blank, and Their Variants

This article explains the Apache Commons Lang StringUtils utility class, detailing the behavior of isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank methods with examples and source code, helping Java developers choose the right check for null, empty, or whitespace strings.

Programmer DD
Programmer DD
Programmer DD
Mastering Apache Commons Lang StringUtils: Empty, Blank, and Their Variants

isEmpty series

StringUtils.isEmpty()

Checks whether a CharSequence is null or has length zero. A string containing only a space is not considered empty.

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
// method implementation
public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
}

StringUtils.isNotEmpty()

Returns the negation of isEmpty.

public static boolean isNotEmpty(final CharSequence cs) {
    return !isEmpty(cs);
}

StringUtils.isAnyEmpty()

Returns true if any of the supplied CharSequences is empty or null.

StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, "foo") = true
StringUtils.isAnyEmpty("", "bar") = true
StringUtils.isAnyEmpty("bob", "") = true
StringUtils.isAnyEmpty(" bob ", null) = true
StringUtils.isAnyEmpty(" ", "bar") = false
StringUtils.isAnyEmpty("foo", "bar") = false
// implementation
public static boolean isAnyEmpty(final CharSequence... css) {
    if (ArrayUtils.isEmpty(css)) {
        return true;
    }
    for (final CharSequence cs : css) {
        if (isEmpty(cs)) {
            return true;
        }
    }
    return false;
}

StringUtils.isNoneEmpty()

Returns true only when none of the supplied CharSequences are empty or null.

public static boolean isNoneEmpty(final CharSequence... css) {
    return !isAnyEmpty(css);
}

isBlank series

StringUtils.isBlank()

Checks whether a CharSequence is null, empty, or contains only whitespace.

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
// implementation
public static boolean isBlank(final CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(cs.charAt(i))) {
            return false;
        }
    }
    return true;
}

StringUtils.isNotBlank()

Negates isBlank.

public static boolean isNotBlank(final CharSequence cs) {
    return !isBlank(cs);
}

StringUtils.isAnyBlank()

Returns true if any supplied CharSequence is blank (null, empty, or whitespace only).

StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, "foo") = true
StringUtils.isAnyBlank(null, null) = true
StringUtils.isAnyBlank("", "bar") = true
StringUtils.isAnyBlank("bob", "") = true
StringUtils.isAnyBlank(" bob ", null) = true
StringUtils.isAnyBlank(" ", "bar") = true
StringUtils.isAnyBlank("foo", "bar") = false
// implementation
public static boolean isAnyBlank(final CharSequence... css) {
    if (ArrayUtils.isEmpty(css)) {
        return true;
    }
    for (final CharSequence cs : css) {
        if (isBlank(cs)) {
            return true;
        }
    }
    return false;
}

StringUtils.isNoneBlank()

Returns true only when none of the supplied CharSequences are blank.

public static boolean isNoneBlank(final CharSequence... css) {
    return !isAnyBlank(css);
}
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaApache Commonsstring handlingStringUtilsutility methodsBlank CheckEmpty Check
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.