isEmpty 和 isBlank 的用法区别,居然一半的人答不上来....._Java技术头条的博客-程序员秘密

技术标签: spring boot  安卓  java  android  js  

点击上方蓝色字体,选择“标星公众号

优质文章,第一时间送达 
大家好,我是燕子!

也许你两个都不知道,也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在, come on ,让我们一起来探索org.apache.commons.lang3.StringUtils;这个工具类.


isEmpty系列

StringUtils.isEmpty()

是否为空. 可以看到 " " 空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isEmpty(" ")=false

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty(“bob”) = false
StringUtils.isEmpty(" bob ") = false
/**
 *
 * <p>NOTE: This method changed in Lang version 2.0.
 * It no longer trims the CharSequence.
 * That functionality is available in isBlank().</p>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is empty or null
 * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
 */
public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
}

StringUtils.isNotEmpty()

相当于不为空 , = !isEmpty()

public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

StringUtils.isAnyEmpty()

是否有一个为空,只有一个为空,就为true.

StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, “foo”) = true
StringUtils.isAnyEmpty("", “bar”) = true
StringUtils.isAnyEmpty(“bob”, “”) = true
StringUtils.isAnyEmpty(" bob ", null) = true
StringUtils.isAnyEmpty(" ", “bar”) = false
StringUtils.isAnyEmpty(“foo”, “bar”) = false
/**
 * @param css  the CharSequences to check, may be null or empty
 * @return {@code true} if any of the CharSequences are empty or null
 * @since 3.2
 */
public static boolean isAnyEmpty(final CharSequence... css) {
  if (ArrayUtils.isEmpty(css)) {
    return true;
  }
  for (final CharSequence cs : css){
    if (isEmpty(cs)) {
      return true;
    }
  }
  return false;
}


StringUtils.isNoneEmpty()

相当于!isAnyEmpty(css) , 必须所有的值都不为空才返回true

/**
 * <p>Checks if none of the CharSequences are empty ("") or null.</p>
 *
 * <pre>
 * StringUtils.isNoneEmpty(null)             = false
 * StringUtils.isNoneEmpty(null, "foo")      = false
 * StringUtils.isNoneEmpty("", "bar")        = false
 * StringUtils.isNoneEmpty("bob", "")        = false
 * StringUtils.isNoneEmpty("  bob  ", null)  = false
 * StringUtils.isNoneEmpty(" ", "bar")       = true
 * StringUtils.isNoneEmpty("foo", "bar")     = true
 * </pre>
 *
 * @param css  the CharSequences to check, may be null or empty
 * @return {@code true} if none of the CharSequences are empty or null
 * @since 3.2
 */
public static boolean isNoneEmpty(final CharSequence... css) {
    

isBank系列

StringUtils.isBlank()

是否为真空值(空格或者空值)

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank(“bob”) = false
StringUtils.isBlank(" bob ") = false
/**
 * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(final CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (Character.isWhitespace(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

StringUtils.isNotBlank()

是否真的不为空,不是空格或者空值 ,相当于!isBlank();

public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

StringUtils.isAnyBlank()

是否包含任何真空值(包含空格或空值)

StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, “foo”) = true
StringUtils.isAnyBlank(null, null) = true
StringUtils.isAnyBlank("", “bar”) = true
StringUtils.isAnyBlank(“bob”, “”) = true
StringUtils.isAnyBlank(" bob ", null) = true
StringUtils.isAnyBlank(" ", “bar”) = true
StringUtils.isAnyBlank(“foo”, “bar”) = false
/**
 * <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
 * @param css  the CharSequences to check, may be null or empty
 * @return {@code true} if any of the CharSequences are blank or null or whitespace only
 * @since 3.2
 */
public static boolean isAnyBlank(final CharSequence... css) {
  if (ArrayUtils.isEmpty(css)) {
    return true;
  }
  for (final CharSequence cs : css){
    if (isBlank(cs)) {
      return true;
    }
  }
  return false;
}

StringUtils.isNoneBlank()

是否全部都不包含空值或空格

StringUtils.isNoneBlank(null) = false
StringUtils.isNoneBlank(null, “foo”) = false
StringUtils.isNoneBlank(null, null) = false
StringUtils.isNoneBlank("", “bar”) = false
StringUtils.isNoneBlank(“bob”, “”) = false
StringUtils.isNoneBlank(" bob ", null) = false
StringUtils.isNoneBlank(" ", “bar”) = false
StringUtils.isNoneBlank(“foo”, “bar”) = true
/**
 * <p>Checks if none of the CharSequences are blank ("") or null and whitespace only..</p>
 * @param css  the CharSequences to check, may be null or empty
 * @return {@code true} if none of the CharSequences are blank or null or whitespace only
 * @since 3.2
 */
public static boolean isNoneBlank(final CharSequence... css) {
  return !isAnyBlank(css);
}

StringUtils的其他方法

可以参考官方的文档,里面有详细的描述,有些方法还是很好用的.

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

20ac310919f3ad41ad71033200a5200a.png bfc751e4873c75769b2ef5c941d08c5d.png

转自:Moshow郑锴

链接:https://blog.csdn.net/moshowgame/article/details/102914895

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chendasheng/article/details/121134040

智能推荐

微信扫描二维码跳转到小程序配置 获取二维码内容_MusiCoder32的博客-程序员秘密

小程序开发 -&gt; 开发管理 -&gt; 开发设置 -&gt; 扫普通链接二维码打开小程序添加跳转规则,可配置项如下图协议类型和选择大小写没啥好说的,前缀占用规则选择占用重点讲以下几条二维码规则:填写备案过的域名加路径的形式如https://xxxxxx.com/myRule检验文件放置需满足该地址(https://xxxxxx.com/校验文件)能访问指定小程序的功能页面在指定页面通过以下方式获取二维码中的内容 onLoad(e) { // 微信扫码后.

c语言实验题水仙花数,c语言综合实验题.ppt_林伯权的博客-程序员秘密

c语言综合实验题.ppt程序设计基础习题 作者 鲍志斌 2. 输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数. 65. 请编程实现:输入几个整数,判断其中偶数的个数,并输出结果. (要求:数据的个数及原始数据由键盘输入) 66. 请编程实现:输入几个整数,判断其中奇数的个数,并输出奇数的累加和. (要求:数据的个数及原始数据由键盘输入) 67. 有一个两位数XY,X是十...

java实现按字段分组_java7&java8针对集合中对象的某一个字段分组_樱桃阳子的博客-程序员秘密

项目中很多地方都会用到分组,提到分组,最简单的就是用java8的stream,但是有些条件下(比方说我,今天)用的java7....fuck..java8用起来比较方便根据普通单个字段//假设传入一个非空订单集合List orderList;//想要按照订单号分组Map&gt; collect = orderList.stream().collect(Collectors.groupingBy(t...

JAVA 静态内部类用法_wangxin0314的博客-程序员秘密

Java之静态内部类(static class) 在一个类中创建另外一个类,叫做成员内部类。这个成员内部类可以静态的(利用static关键字修饰),也可以是非静态的。 一、静态内部类的使用目的。在 定义内部类的时候,在其前面加上一个权限修饰符static。这个内部类就变为了静态内部类。如在进行代码程序测试的时候,如果在每一个Java源文件中 都设置一

快速理解编程结构_如何快速理解Qt For Python的模块结构? | 州的先生_weixin_39723899的博客-程序员秘密

点击关注州的先生编程应用、实战教程,不容错过在安装完成PyQt5/PySide2之后,我们先不急着开始进行Python图形界面的编程。因为现在我们对PyQt5和PySide2还一无所知。PyQt5和PySide2都是QT图形界面开发包的Python封装模块,它们最大程度地利用Python编程语言还原了QT在原生C++下实现的功能和定义。所以我们使用PyQt5或是PySide2都可以较完整...

随便推点

SQL中合并多行记录的方法总汇_sql 将两个表的数据合并为多行_dobear_0922的博客-程序员秘密

 -- =============================================================================-- Title: 在SQL中分类合并数据行-- Author: dobear        Mail(MSN): [email protected] Environment: Vista + SQL2005-- 

星球年度汇总-爬虫获取股票数据A—概览篇_元宵大师的博客-程序员秘密

量化交易是一个多技术综合的项目,学习完书籍《Python股票量化交易从入门到实践》我们提供了升级的学习内容——知识星球《玩转股票量化交易》在星球中我们深入分享包括Python、爬虫、数据...

一场疫情,炸出了退休的COBOL程序员_云水木石的博客-程序员秘密

COBOL编程语言,估计大多数程序员从没听说过,我这样的编程老司机,也是只闻其名,从未一睹芳容。出门问了问度娘,答案如下:COBOL语言,是一种面向过程的高级程序设计语言,主要用于数据...

理财方案:工薪一族月薪2 000元的理财计划 _escode的博客-程序员秘密

理财方案工薪一族月薪2 000元的理财计划现在有很多的大学生都是在毕业以后选择留在自己上学的城市,一来对城市有了感情,二来也希望能在大的城市有所 发展,而现在很多大城市劳动力过剩,大学生想找到一个自己喜欢又有较高收入的职位已经变得非常难,很多刚毕业的朋友的月收入都可能徘徊在2 000元人民 币左右,如果您是这样的情况,让我们来核算一下,如何利用手中的有限资金来进行理财。如果您是单身一

SEO初级|网站代码优化—img标记的alt属性、H标记、Strong标记(强调)_naive_sonaive的博客-程序员秘密

一、、img标记的alt属性&lt;img src=”123.jpg”alt=”宇宙大爆炸图片效果”/&gt;Alt属性撰写技巧:Alt属性包含关键词Alt属性值不能为单一的关键词,应放置在一句描述中;Alt属性关键词不可罗列堆积文章中图片的Alt属性建议使用文章标题,作为属性值——既可增加关键词频度,又可以合理控制关键词密度。练习:给文章中的图片增加Alt属性二、、H标记...