Backend Development 7 min read

Understanding Apache Commons Lang StringUtils isEmpty/isBlank Methods in Java

This article explains the various Apache Commons Lang StringUtils methods such as isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank, providing usage examples, code snippets, and detailed behavior for handling null, empty, and whitespace strings in Java.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Apache Commons Lang StringUtils isEmpty/isBlank Methods in Java

StringUtils isEmpty Series

The isEmpty method checks whether a CharSequence is null or has length zero; a single 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:

/**
 * NOTE: This method changed in Lang version 2.0.
 * It no longer trims the CharSequence. That functionality is available in isBlank().
 * @param cs the CharSequence to check, may be null
 * @return true if the CharSequence is empty or null
 */
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
/**
 * @param css the CharSequences to check, may be null or empty
 * @return true if any of the CharSequences are empty or null
 */
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‑empty and non‑null.

/**
 * Checks if none of the CharSequences are empty ("") or null.
 */
public static boolean isNoneEmpty(final CharSequence... css) {
    // implementation omitted for brevity – it returns !isAnyEmpty(css)
}

StringUtils isBlank Series

StringUtils.isBlank()

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

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
/**
 * Checks if a CharSequence is whitespace, empty ("") or null.
 */
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()

Equivalent to !isBlank(cs) , i.e., the string contains non‑whitespace characters.

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
/**
 * Checks if any one of the CharSequences are blank ("") or null and 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 all supplied CharSequences are non‑blank (no null, empty, or whitespace only).

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

Other StringUtils Methods

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

Apache Commons Lang StringUtils Javadoc
Backend DevelopmentUtility LibraryString handlingStringUtilsApache Commons Lang
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.