三年前,在之前公司上班时,碰到了一个工作三年的程序员,他居然没搞懂isEmpty和isBlank两者的区别,其实我感觉挺无语的,这两个方法在日常开发中应该经常的使用,很熟悉吧!

也许你两个都不知道,也许你除了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) {{return !isAnyEmpty(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()

纯净版后台管理系统,基于 Spring Boot、Spring Security、JWT 后端框架,Vue & Element 前端框架的前后端分离的用户权限管理系统,代码易读易懂、界面简洁美观。其核心技术采用 Spring、MyBatis、Shiro 没有任何其它过度依赖包,下载即可运行使用。

https://gitee.com/yoodb/jing-xuan

是否真的不为空,不是空格或者空值,相当于!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()

是否全部都不包含空值或空格。面试宝典:https://www.yoodb.com/

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

作者:Moshow郑锴

blog.csdn.net/moshowgame/article/details/102914895

公众号“Java精选”所发表内容注明来源的,版权归原出处所有(无法查证版权的或者未注明出处的均来自网络,系转载,转载的目的在于传递更多信息,版权属于原作者。如有侵权,请联系,笔者会第一时间删除处理!
最近有很多人问,有没有读者交流群!加入方式很简单,公众号Java精选,回复“加群”,即可入群!Java精选面试题(微信小程序):3000+道面试题,包含Java基础、并发、JVM、线程、MQ系列、Redis、Spring系列、Elasticsearch、Docker、K8s、Flink、Spark、架构设计等,在线随时刷题!
------ 特别推荐 ------
特别推荐:专注分享最前沿的技术与资讯,为弯道超车做好准备及各种开源项目与高效率软件的公众号,「大咖笔记」,专注挖掘好东西,非常值得大家关注。点击下方公众号卡片关注。点击“阅读原文”,了解更多精彩内容!文章有帮助的话,点在看,转发吧!

isEmpty和isBlank的用法区别,至少一半的人答不上来...相关推荐

  1. isEmpty 和 isBlank 的用法区别

    isEmpty 和 isBlank 的用法区别 isEmpty系列 一.StringUtils.isEmpty() 1.用此方法首先得引入依赖 <dependency><groupI ...

  2. isEmpty 和 isBlank 的用法区别,居然一半的人答不上来.....

  3. java的isEmpty与isBlank的用法

    废话不多说直接上代码 // 下面是StringUtils.isEmpty判断是否为空的示例: StringUtils.isEmpty(null) = true; StringUtils.isEmpty ...

  4. Java的isEmpty和isBlank

    在刚接触java的时候,在字符串判断非空的时候,习惯性的使用equals来判空,有时候效果还不是很好,后来使用了StringUtils下的isEmpty和isBlank来判空,那这两者又什么区别呢? ...

  5. 是否注意过 isEmpty 和 isBlank 区别?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 转自:简书,作者:希希里之海 www.jianshu.com/p/ ...

  6. 是否注意过isEmpty 和 isBlank 区别?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://h5ip.cn/ix9z 前言 org.apa ...

  7. java script isblank_java判断一个字符串是否为空,isEmpty和isBlank的区别

    转载于:https://blog.csdn.net/liusa825983081/article/details/78246792 实际应用中,经常会用到判断字符串是否为空的逻辑 比较简单的就是用 S ...

  8. pyqt5 判断lineedit是否为空_是否注意过 isEmpty 和 isBlank 区别?

    (给ImportNew加星标,提高Java技能) 转自:简书,作者:希希里之海 www.jianshu.com/p/98e7593ca0e2 前言 org.apache.commons.lang.St ...

  9. java判断一个字符串是否为空,isEmpty和isBlank的区别

    实际应用中,经常会用到判断字符串是否为空的逻辑 比较简单的就是用 Str != null && Str.length() >0 来判断 其实很多java工具集都是有包装好的接口可 ...

最新文章

  1. 除了 AI,这些技术为 IIoT 插上飞向“4.0”的翅膀
  2. AI公开课:19.05.29 浣军-百度大数据实验室主任《AutoDL 自动化深度学习建模的算法和应用》课堂笔记以及个人感悟
  3. python爬取论坛帖子_python爬虫爬取虎扑论坛的帖子名称和链接,为什么只能爬10页就报...
  4. 百度 AI 再发福利!不仅有实战营,还有手把手教学的“深度学习公开课”
  5. Html5游戏开发-145行代码完成一个RPG小Demo
  6. [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)
  7. java nio ppt_Java开发基础知识讲解.ppt
  8. DN安卓2014版(5-9)
  9. 数字媒体技术和数据科学与大数据技术_数据科学与大数据技术专业的女同学,迈出了她的舒适圈...
  10. python批量查询IP物理地址输出到Exel
  11. oracle sqlplus
  12. Android建快捷方式app,创建快捷方式最新版下载-创建快捷方式appv1.17 安卓版-腾牛安卓网...
  13. 开源又好用的录屏软件
  14. L1-044 稳赢 (15 分)(JAVA)
  15. 线性代数和微积分 1.1微分方程概述
  16. java里SQL insert操作的语法_Java含个人总结语法:JDBC,学生表,实体类,集合,增删改查,注入,预处理【诗书画唱】...
  17. bps pps fps的定义
  18. doPost请求的用法
  19. EMAX银燕舵机的控制
  20. 用爱思助手自签名ipa文件成功后安装失败

热门文章

  1. 找人要代码的邮件怎么写
  2. 罗马时钟 基于前端CSS、JS 实现
  3. java说明文档的制作
  4. 学计算机毕业都需要熬夜吗,大学毕业后经常加班的几个专业,996是常态,熬夜加班也很普遍...
  5. 苏州睿创 2022 面试题
  6. 人工智能如何配合商业采购策略?
  7. UCOS2系统内核讲述(三)_TCB任务控制块
  8. delphi xe 之路(29)安装xe7up1
  9. Django学习记录之——choices参数
  10. 使用动画编辑器编辑、绘制复杂的精灵动画