1. 字符串比较
StringUtils.equals(null,null)=true
StringUtils.equals(“abc”,null)=false
StringUtils.equals(null,”abc”)=false
StringUtils.equals(“abc”,”abc”)=true
StringUtils.equals(“abc”,”ABC”)=false
2. 集合工具类判空
List<xxDto> resultList=XXX
CollectionUtil.isEmpty(resultList){}
3. 对象类型判空
People p = xxService.selectXX();//结果为People类型的p,判断结果p是否为空
Objects.isNull(p)
4. 字符串工具类比较、判空
(1)比较
StrUtil.equals(“abc”,”abc”);结果为true--->比较两个字符串(大小写敏感:A≠a)
StrUtil.equals(“abc”,”ABC”);结果为false
(2)判空
String id = XXX;
StrUtil.isBlank(id)-->StrUtil.isBlank判空,(空格、全角空格、制表符、换行符等不可见字符都为true)
StrUtil.isBlank(“”)-->true
StrUtil.isBlank(null)-->true
StrUtil.isBlank(“\t\n”)-->true
StrUtil.isBlank(“abc”)-->false
StrUtil.isBlank和StrUtil.isEmpty的区别是:该方法会校验空白字符,且性能相对于isEmpty略慢
建议:该方法建议仅对于客户端(或第三方接口)穿入的参数使用该方法;
需要同时校验多个字符串时,建议采用hasBlank或者isAllBlank
判断是否不为空
StringUtils.isNotBlank(null);-->false
StringUtils.isNotBlank("");-->false
StringUtils.isNotBlank(" ");-->false
StringUtils.isNotBlank("abc");-->true
StringUtils.isNotBlank(" abc ");-->true
-----------------------------------------------------
StringUtils.isNotEmpty(null);-->false
StringUtils.isNotEmpty("");-->false
StringUtils.isNotEmpty(" ");-->true
StringUtils.isNotEmpty("abc");-->true
StringUtils.isNotEmpty(" abc ");-->true
判断是否为空
StringUtils.isEmpty只有为null或者是empty的时候才为true,空格也会判为非空
StringUtils.isEmpty(null);-->true
StringUtils.isEmpty("");-->true
StringUtils.isEmpty(" ");-->false
StringUtils.isEmpty("abc");-->false
StringUtils.isEmpty(" abc ");-->false
------------------------------------------
StringUtils.isBlank只有为null或者是空格的时候才为true
StringUtils.isBlank(null);-->true
StringUtils.isBlank("");-->false
StringUtils.isBlank(" ");-->true
StringUtils.isBlank("abc");-->false
StringUtils.isBlank(" abc ");-->false