Mastering Apache Commons Lang StringUtils: Empty, Blank, and Their Variants
This article explains the Apache Commons Lang StringUtils utility class, focusing on the isEmpty/isNotEmpty and isBlank/isNotBlank families, their behavior with null, empty strings and whitespace, provides concrete code examples, and lists other useful StringUtils methods for Java developers.
The org.apache.commons.lang3.StringUtils class provides null‑safe utilities for common string checks and transformations in Java.
isEmpty Series
StringUtils.isEmpty returns true when the supplied CharSequence is null or has a length of zero. Whitespace characters are considered non‑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 is the logical negation of isEmpty.
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}StringUtils.isAnyEmpty returns true if **any** of the provided CharSequence arguments is null or empty. An empty array is also treated as 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 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** arguments are non‑empty; it is equivalent to !isAnyEmpty(css).
public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}isBlank Series
StringUtils.isBlank treats null, empty strings, and strings consisting solely of whitespace as blank.
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) {
if (cs == null) {
return true;
}
int strLen = cs.length();
if (strLen == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}StringUtils.isNotBlank is the logical negation of isBlank.
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}StringUtils.isAnyBlank returns true if **any** argument is 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;
}StringUtils.isNoneBlank returns true only when none of the arguments are blank; it is equivalent to !isAnyBlank(css).
public static boolean isNoneBlank(final CharSequence... css) {
return !isAnyBlank(css);
}Other Frequently Used Methods
Beyond the empty/blank checks, StringUtils offers utilities for trimming, case conversion, searching, joining, splitting, padding, counting matches, and computing Levenshtein distance, among many others. A non‑exhaustive list includes:
trim/strip – remove leading and trailing whitespace.
equals / compare – null‑safe equality checks.
startsWith / endsWith – prefix and suffix checks.
contains / indexOf / lastIndexOf – null‑safe substring searches.
substring / left / right / mid – safe substring extraction.
join / split – convert between strings and arrays.
replace / overlay – replace occurrences of a substring.
appendIfMissing / prependIfMissing – add a suffix or prefix only if absent.
leftPad / rightPad / center / repeat – pad or repeat strings.
upperCase / lowerCase / swapCase / capitalize / uncapitalize – case transformations.
countMatches – count occurrences of a substring.
isAlpha / isNumeric / isWhitespace / isAsciiPrintable – character class checks.
defaultString – provide a default for null inputs.
rotate – circular shift of characters.
reverse / reverseDelimited – reverse a string or delimited sections.
abbreviate – shorten a string with ellipsis.
difference – report the first differing character between two strings.
levenshteinDistance – compute edit distance between two strings.
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
