StringUtils类中的isEmpty和isBlank的区别
- org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String str)。
1. StringUtils.isEmpty()
- StringUtils.isEmpty()判断str为空的条件是:
- str==null
- str.length() == 0
- 所以如果str为多个空格的话,是不会判断为空的
System.out.println(StringUtils.isEmpty(null)); //true
System.out.println(StringUtils.isEmpty("")); //true
System.out.println(StringUtils.isEmpty(" ")); //false
System.out.println(StringUtils.isEmpty("dd")); //false
2. StringUtils.isBlank()
- StringUtils.isEmpty()判断str为空的条件是:
- str == “空格”
- str == null
- str.length() == 0
- 所以如果str为多个空格的话,也是会判断为空的
System.out.println(StringUtils.isBlank(null)); //true
System.out.println(StringUtils.isBlank("")); //true
System.out.println(StringUtils.isBlank(" ")); //true
System.out.println(StringUtils.isBlank("dd")); //false
3.总结
其实根据名字就可以看出来,isBlank意思就是判断是否为空白,所以空格就会算成空。