I am trying to check if a Java String is not null, not empty and not whitespace.
In my mind, this code should have been quite up for the job.
public static boolean isEmpty(String s) {
if ((s != null) && (s.trim().length() > 0))
return false;
else
return true;
}
As per documentation, String.trim() should work thus:
Returns a copy of the string, with leading and trailing whitespace omitted.
If this
Stringobject represents an empty character sequence, or the first and last characters of character sequence represented by thisStringobject both have codes greater than'\u0020'(the space character), then a reference to thisStringobject is returned.
However, apache/commons/lang/StringUtils.java does it a little differently.
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
As per documentation, Character.isWhitespace():
Determines if the specified character is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria:
- It is a Unicode space character (
SPACE_SEPARATOR,LINE_SEPARATOR, orPARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0','\u2007','\u202F').- It is
'\t', U+0009 HORIZONTAL TABULATION.- It is
'\n', U+000A LINE FEED.- It is
'\u000B', U+000B VERTICAL TABULATION.- It is
'\f', U+000C FORM FEED.- It is
'\r', U+000D CARRIAGE RETURN.- It is
'\u001C', U+001C FILE SEPARATOR.- It is
'\u001D', U+001D GROUP SEPARATOR.- It is
'\u001E', U+001E RECORD SEPARATOR.- It is
'\u001F', U+001F UNIT SEPARATOR.
If I am not mistaken - or might be I am just not reading it correctly - the String.trim() should take away any of the characters that are being checked by Character.isWhiteSpace(). All of them see to be above '\u0020'.
In this case, the simpler isEmpty function seems to be covering all the scenarios that the lengthier isBlank is covering.
- Is there a string that will make the
isEmptyandisBlankbehave differently in a test case? - Assuming there are none, is there any other consideration because of which I should choose
isBlankand not useisEmpty?
For those interested in actually running a test, here are the methods and unit tests.
public class StringUtil {
public static boolean isEmpty(String s) {
if ((s != null) && (s.trim().length() > 0))
return false;
else
return true;
}
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
}
And unit tests
@Test
public void test() {
String s = null;
assertTrue(StringUtil.isEmpty(s)) ;
assertTrue(StringUtil.isBlank(s)) ;
s = "";
assertTrue(StringUtil.isEmpty(s)) ;
assertTrue(StringUtil.isBlank(s));
s = " ";
assertTrue(StringUtil.isEmpty(s)) ;
assertTrue(StringUtil.isBlank(s)) ;
s = " ";
assertTrue(StringUtil.isEmpty(s)) ;
assertTrue(StringUtil.isBlank(s)) ;
s = " a ";
assertTrue(StringUtil.isEmpty(s)==false) ;
assertTrue(StringUtil.isBlank(s)==false) ;
}
Update: It was a really interesting discussion - and this is why I love Stack Overflow and the folks here. By the way, coming back to the question, we got:
- A program showing which all characters will make the behave differently. The code is at https://ideone.com/ELY5Wv. Thanks @Dukeling.
- A performance related reason for choosing the standard
isBlank(). Thanks @devconsole. - A comprehensive explanation by @nhahtdh. Thanks mate.
nullis accepted and will returntrue.&&is correctpublic static boolean isEmpty(String s) { return (s == null) || s.trim().isEmpty(); }. I am always somewhat confused if someone uses a whole if statement to express just a negation.