Fundamentals 7 min read

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

This article introduces the Apache Commons Lang StringUtils class, demonstrating how its isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank methods work with practical code examples and explains their behavior for different input scenarios.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Exploring Apache Commons Lang StringUtils: isEmpty, isBlank, and Related Methods

The article presents a concise guide to the org.apache.commons.lang3.StringUtils utility class, focusing on its string‑checking methods and providing concrete examples for each.

isEmpty series

StringUtils.isEmpty(null) = true

StringUtils.isEmpty("") = true

StringUtils.isEmpty(" ") = false

Implementation:

public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
}

isNotEmpty

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

isAnyEmpty returns true if any supplied CharSequence 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

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

isNoneEmpty is the logical opposite of isAnyEmpty – it returns true only when all inputs are non‑empty.

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

isBlank series

StringUtils.isBlank(null) = true

StringUtils.isBlank("") = true

StringUtils.isBlank(" ") = true

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

isNotBlank simply negates isBlank :

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

isAnyBlank checks whether any argument 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

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

isNoneBlank returns true only when all inputs are non‑blank.

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

The article concludes by recommending the official Apache Commons Lang documentation for a complete reference.

Reference: Apache Commons Lang StringUtils Javadoc

JavaApache CommonsString handlingStringUtilsUtility methods
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.