一,首先看源码里isEmpty和isBlank是怎么定义的.
/**
*isEmpty的方法
*/
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
/**
*isBlank的方法
*/
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs != null && (strLen = cs.length()) != 0) {
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
二,由源码可以看出
isEmpty只对字符串是否是null和字符串是否是“”做了判断,并没有对“ ”做判断。测试代码如下
String a="";
String b=" ";
String c=null;
System.out.println(StringUtils.isEmpty(a));//true
System.out.println(StringUtils.isEmpty(b));//false
System.out.println(StringUtils.isEmpty(c));//true
isBlank对字符串是否是null和字符串是否是“”做了判断,也对“ ”做判断。测试代码如下
String a="";
String b=" ";
String c=null;
System.out.println(StringUtils.isBlank(a));//true
System.out.println(StringUtils.isBlank(b));//true
System.out.println(StringUtils.isBlank(c));//true
//完毕