全面认识lang3包

jar包:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

1、StringUtils判空方法

源码 

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

public static boolean isEmpty(CharSequence cs) {
    return cs == null || cs.length() == 0;
}
String strNull = null;
System.out.println("strNull isBlank:" + StringUtils.isBlank(strNull) + ",isEmpty:" + StringUtils.isEmpty(strNull));
String str = "";
System.out.println("str\"\" isBlank:" + StringUtils.isBlank(str) + ",isEmpty:" + StringUtils.isEmpty(str));
String strKongge = " ";
System.out.println("strKongge isBlank:" + StringUtils.isBlank(strKongge) + ",isEmpty:" + StringUtils.isEmpty(strKongge));
String strEmPTY = StringUtils.EMPTY;
System.out.println("strEmPTY isBlank:" + StringUtils.isBlank(strEmPTY) + ",isEmpty:" + StringUtils.isEmpty(strEmPTY));
String strSpe1 = "\t";
System.out.println("strSpe1\\t isBlank:" + StringUtils.isBlank(strSpe1) + ",isEmpty:" + StringUtils.isEmpty(strSpe1));
String strSpe2 = "\n";
System.out.println("strSpe2\\n isBlank:" + StringUtils.isBlank(strSpe2) + ",isEmpty:" + StringUtils.isEmpty(strSpe2));
String strSpe3 = "\n\t";
System.out.println("strSpe3\\n\\t isBlank:" + StringUtils.isBlank(strSpe3) + ",isEmpty:" + StringUtils.isEmpty(strSpe3));
strNull isBlank:true,isEmpty:true
str"" isBlank:true,isEmpty:true
strKongge isBlank:true,isEmpty:false
strEmPTY isBlank:true,isEmpty:true
strSpe1\t isBlank:true,isEmpty:false
strSpe2\n isBlank:true,isEmpty:false
strSpe3\n\t isBlank:true,isEmpty:false

2、集合判空CollectionUtils.isEmpty

jar包:

public static boolean isEmpty(Collection coll) {
   return (coll == null || coll.isEmpty());
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值