Mastering Apache Commons StringUtils: When to Use isEmpty, isBlank, and More

This article explains the differences among Apache Commons Lang's StringUtils methods—such as isEmpty, isBlank, isNotEmpty, isAnyEmpty, isNoneEmpty, and their variants—providing code examples, usage guidelines, and practical tips for Java developers to avoid common pitfalls when handling empty or blank strings.

macrozheng
macrozheng
macrozheng
Mastering Apache Commons StringUtils: When to Use isEmpty, isBlank, and More

isEmpty series

StringUtils.isEmpty()

Determines whether a CharSequence is null or has length zero. Note that 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
public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
}

StringUtils.isNotEmpty()

Returns the opposite of isEmpty.

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

StringUtils.isAnyEmpty()

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

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
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 null or empty.

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

isBlank series

StringUtils.isBlank()

Checks whether a CharSequence is null, empty, or consists solely of whitespace.

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
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 null, empty, or whitespace only.

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);
}

For a complete list of StringUtils methods, refer to the official Apache Commons Lang documentation.

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 CommonsStringUtilsisEmptyisBlank
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.