org.springframework.util
StringUtils.isEmpty
public static boolean isEmpty(@Nullable Object str){
return str == null || "".equals(str);
}
判断字符串是否为空,如果为null或者“”则返回true,否则返回false,
org.apache.commons.lang3
StringUtils.isEmpty
Public static boolean isEmpty(CharSequence cs){
return cs == null || cs.length()==0;
}
StringUtils.isBlank
public static boolean isBlank(CharSequence cs){
int strlen=length(cs);
if(strlen == 0){
return true;
}else{
for(int i = 0;i < strlen; ++i){
if(!Character.isWhitespace(cs.charAt(i))){
return false;
}
}
return true;
}
}
两者都是判断字符串是否为空的方法,区别是对空格字符的判断
对于isEmpty来说,“ "不是空
对于isBlank来说,“ ”是空
null表示这个字符串不指向任何的东西,如果这时候调用它的方法,那么就会出现空指针一样
“”表示它指向一个长度为0 的字符串,这时候调用它的方法是安全的
null不是对象,“” 是对象,所以null没有分配空间,“”分配了空间
List判空
if(!CollectionUtils.isEmpty(applyMealRecordEntities)){
//不为空的情况
}else{
//为空的情况(比如,这个日期还没人报餐)
return null;
}
都是细节,要用心