Understanding Apache Commons StringUtils isEmpty, isBlank and Related Methods
This article explains the Apache Commons Lang3 StringUtils utility class, detailing the behavior, usage examples, and source code of isEmpty, isNotEmpty, isAnyEmpty, isNoneEmpty, isBlank, isNotBlank, isAnyBlank, and isNoneBlank methods for Java developers.
The article introduces the Apache Commons Lang3 StringUtils utility class, focusing on methods that check emptiness and blankness of CharSequence objects, which many Java developers may misuse.
isEmpty series
StringUtils.isEmpty(CharSequence) returns true for null or zero‑length strings, but not for strings containing only whitespace. Example results are shown.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = falseThe source code of isEmpty is provided.
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}StringUtils.isNotEmpty() is simply the negation of isEmpty .
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}StringUtils.isAnyEmpty(...) returns true if any of the supplied CharSequence arguments is empty or null , with examples and 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(...) is the opposite of isAnyEmpty , requiring all inputs to be non‑empty.
public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}isBlank series
StringUtils.isBlank(CharSequence) treats null , empty string, or a string consisting solely of whitespace as true . Sample outputs are listed.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = falseThe implementation checks for null , zero length, or only whitespace characters.
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)) == false) {
return false;
}
}
return true;
}StringUtils.isNotBlank() is the negation of isBlank .
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}StringUtils.isAnyBlank(...) returns true if any argument is blank; StringUtils.isNoneBlank(...) returns true only when none are blank.
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;
}
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}The article concludes by directing readers to the official Apache Commons Lang documentation for further details on additional StringUtils methods.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.