Spring的StringUtils和commons的StringUtils中isEmpty的区别

Spring的isEmpty方法用于判断对象是否为null或字符串是否为空,而commons的isEmpty仅判断字符串是否为null或长度为0。此外,commons提供了isBlank方法,考虑了空格和制表符的情况,更适用于实际开发中的空白字符检查。Spring的方法接受Object参数,适合处理各种类型,而commons的方法限定为String类型。

Spring中的isEmpty:判断str是否为null或是否为空字符串。

public static boolean isEmpty(@Nullable Object str) {
        return str == null || "".equals(str);
    }

 commons中的isEmpty:isEmpty() 方法没有忽略空格,是以是否为空和是否存在为判断依据

public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

但commons中又补充了isBlank方法:isBlank() 方法增加了字符串为空格、制表符的判断。即isBlank()的判断范围更大,它在isEmpty()方法的基础上,包括了空字符的判断。在实际开发中,isBlank()方法更加常用

    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

很显然,spring中的isEmpty可以接收Object类型的参数,而commons中的isEmpty只能接受String,日常开发中我们对实体类的id字段,age字段等大多使用包装类Integer,如果使用commons中的isEmpty判断在编写时会爆红

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值