Fundamentals 6 min read

Understanding Apache Commons Lang StringUtils: isEmpty, isBlank, and Related Methods

This article explains the behavior and differences of Apache Commons Lang StringUtils methods such as isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank, providing usage examples, return values, and source code snippets for each method.

Architecture Digest
Architecture Digest
Architecture Digest
Understanding Apache Commons Lang StringUtils: isEmpty, isBlank, and Related Methods

isEmpty Series

StringUtils.isEmpty()

Checks whether a CharSequence is null or has a length of zero. Note that a string containing only a space character is not considered empty.

StringUtils.isEmpty(null)    = true
StringUtils.isEmpty("")      = true
StringUtils.isEmpty(" ")     = false
StringUtils.isEmpty("bob")   = false
StringUtils.isEmpty(" bob ") = false

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 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 all supplied CharSequences are non‑null and non‑empty (i.e., the negation of isAnyEmpty() ).

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 characters.

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 of the supplied CharSequences is 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
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 null, empty, or whitespace only (i.e., the negation of isAnyBlank() ).

public static boolean isNoneBlank(final CharSequence... css) {
    return !isAnyBlank(css);
}

For a complete reference, see the official Apache Commons Lang documentation.

JavaString handlingStringUtilsUtility methodsApache Commons Lang
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login 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.