String str1 = "Hello"; boolean empty1 = StringUtils.isEmpty(str1); // false String str2 = ""; boolean empty2 = StringUtils.isEmpty(str2); // true String str3 = null; boolean empty3 = StringUtils.isEmpty(str3); // true
String str = " "; boolean empty = StringUtils.isEmpty(str.trim()); // trueIn this example, we have a string that consists of only whitespace characters. We use the trim() method to remove the whitespace from both ends of the string, then pass the result to isEmpty(). It returns true, because the resulting string has a length of zero. The StringUtils class is part of the org.apache.commons.lang3 package, which is a library of reusable Java code for common programming tasks.