Checks if a String is empty (""), null and whitespace only in Java
Description
The following code shows how to checks if a String is empty (""), null and whitespace only.
Example
/*from w ww.j a v a2 s . co m*/
public class Main {
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;
}
public static void main(String[] argv){
System.out.println(isBlank("java2s.com"));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »