isEmpty和StringUtils.isBlank()的比较

文章目录

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()功能更加全面,因此推荐使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值