1.代码
注意导入的是
import org.apache.commons.lang3.StringUtils;
String test1 = "";
String test2 = " ";
String test3 = new String();
String test4 = null;
String test5 = "\n";
String test6 = "\t";
String test7 = "a";
//isEmpty()的测试
System.out.println("----------------isEmpty()---------------");
System.out.println("test1.isEmpty() = " + test1.isEmpty());
System.out.println("test2.isEmpty() = " + test2.isEmpty());
//报空指针异常
//System.out.println("test3.isEmpty() = " + test3.isEmpty());
//System.out.println("test4.isEmpty() = " + test4.isEmpty());
System.out.println("test5.isEmpty() = " + test5.isEmpty());
System.out.println("test6.isEmpty() = " + test6.isEmpty());
System.out.println("test7.isEmpty() = " + test7.isEmpty());
//StringUtils.iEmpty()的测试
System.out.println("----------------StringUtils.isEmpty()---------------");
System.out.println("test1.isEmpty() = " + StringUtils.isEmpty(test1));
System.out.println("test2.isEmpty() = " + StringUtils.isEmpty(test2));
System.out.println("test3.isEmpty() = " + StringUtils.isEmpty(test3));
System.out.println("test4.isEmpty() = " + StringUtils.isEmpty(test4));
System.out.println("test5.isEmpty() = " + StringUtils.isEmpty(test5));
System.out.println("test6.isEmpty() = " + StringUtils.isEmpty(test6));
System.out.println("test7.isEmpty() = " + StringUtils.isEmpty(test7));
//StringUtils.isBlank()的测试
System.out.println("----------------StringUtils.isBlank()----------------");
System.out.println("test1.isEmpty() = " + StringUtils.isBlank(test1));
System.out.println("test2.isEmpty() = " + StringUtils.isBlank(test2));
System.out.println("test3.isEmpty() = " + StringUtils.isBlank(test3));
System.out.println("test4.isEmpty() = " + StringUtils.isBlank(test4));
System.out.println("test5.isEmpty() = " + StringUtils.isBlank(test5));
System.out.println("test6.isEmpty() = " + StringUtils.isBlank(test6));
System.out.println("test7.isEmpty() = " + StringUtils.isBlank(test7));
结果
----------------isEmpty---------------
test1.isEmpty() = true
test2.isEmpty() = false
回车:test5.isEmpty() = false
制表:test6.isEmpty() = false
test7.isEmpty() = false
----------------isBlank----------------
test1.isEmpty() = true
test2.isEmpty() = true
test3.isEmpty() = true
test4.isEmpty() = true
test5.isEmpty() = true
test6.isEmpty() = true
test7.isEmpty() = false
2.比较
isEmpty()仅能够检测出"“,甚至不能够检测出” "。
在内容为null时,还会报空指针异常,也无法检测制表符和换行符。
StringUtils.isEmpty()能够检测"",null和new String(),但是也有不足。
StringUtils.isBlank()功能更加全面,因此推荐使用。
514

被折叠的 条评论
为什么被折叠?



