两者都检验字符串是否为空,但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;
}
}
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
从源码看出:
isEmpty()忽略空格参数,是以是否为空和是否存在作为判断依据。
isBlank()则可以校验三种情况:是否为null、是否为""、是否为空字符串(引号中间有空格)eg" "、制表符、换行符、换页符和回车。
开发常用方法,特做记录