详解StringUtils中的isEmpty,isBlank和isEmpty,!isEmpty的区别

本文深入探讨了在Java中使用StringUtils工具类进行字符串空值判断的方法,通过具体示例对比了isEmpty、isNotEmpty和isBlank等方法的区别,并展示了如何避免NullPointerException。了解这些技巧将帮助开发者更高效地处理字符串操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 @Test
    public void test(){
        String str1="";
        String str2=" ";
        String str3=null;
        
        System.out.println("1" + StringUtils.isEmpty(str1)); //true
        System.out.println("2" + StringUtils.isEmpty(str2)); //false
        System.out.println("3" + StringUtils.isEmpty(str3)); //true
        
        System.out.println("4" + StringUtils.isNotEmpty(str1)); //false
        System.out.println("5" + StringUtils.isNotEmpty(str2)); //true
        System.out.println("6" + StringUtils.isNotEmpty(str3)); //false
        
        System.out.println("7" + StringUtils.isBlank(str1)); //true
        System.out.println("8" + StringUtils.isBlank(str2)); //true
        System.out.println("9" + StringUtils.isBlank(str3)); //true
        
        System.out.println("10" + str1.isEmpty());//true
        System.out.println("11" + str2.isEmpty());//false
        System.out.println("12" + str3.isEmpty());//java.lang.NullPointerException
       
        System.out.println("13" + !str1.isEmpty());//false
        System.out.println("14" + !str2.isEmpty());//true
        System.out.println("15" + !str3.isEmpty());//java.lang.NullPointerException
    }

StringUtils来自

import org.apache.commons.lang3.StringUtils;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值