java的StringUtils.isBlank和StringUtils.isEmpty方法区别(org.apache.commons.lang3.StringUtils)
前言
估计很多朋友跟我一样,平时也不会特别去注意究竟用isBlank
还是isEmpty
去判断空字符串,但是大部分场景优先使用isBlank
就对了。
- isEmpty是否为空,只有当
==null
或者==""
才为空 - isBlank是否为真空,
==null
和==""
以及各种长度的空格==" "
都为空
而且除了isEmpty/isNotEmpty/isNotBlank/isBlank外,其实还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank让我们一起来研究一下org.apache.commons.lang3.StringUtils
这个工具类吧。
isBank()
是否为真空值(空格或者空值)
- StringUtils.isBlank(null) = true
- StringUtils.isBlank("") = true
- StringUtils.isBlank(" ") = true
- StringUtils.isBlank(" ") = true
- StringUtils.isBlank("moshow") = false
- StringUtils.isBlank(" moshow ") = 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, " ") = true
StringUtils.isAnyBlank("", " ") = true
/*** <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, " ") = false
StringUtils.isNoneBlank("", " ") = false
StringUtils.isNoneBlank("moshow", " moshow ") = true
关于其他方法其实都是以此类推,知道这几个,就可以类推对应的is*Empty等等鞥,这里就不做重复展示。
/*** <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
方法 (整理 by https://zhengkai.blog.csdn.net/ )
|
说明EN | 说明CN |
---|---|---|
IsEmpty/IsBlank | checks if a String contains text | 判断是否包含文本 |
Trim/Strip | removes leading and trailing whitespace | 删除多余空格 |
Equals/Compare | compares two strings in a null | safe manner |
startsWith /endsWith | check if a String starts/ends with a prefix | 判断是什么开头/结尾 |
IndexOf/LastIndexOf/Contains | contain a string and return the index | 判断字符串在string中的位置,判断是否包含 |
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut | index of any of a set of Strings | 如果包含某几个字符串就返回 |
ContainsOnly/ContainsNone/ContainsAny | checks if String contains only/none/any of these characters | |
Substring/Left/Right/Mid/SubstringBefore/SubstringAfter/SubstringBetween | safe substring extractions | 截取字符串方法 |
Split/Join | splits a String into an array of substrings and vice versa | 切割或者拼接字符串 |
Remove/Delete | removes part of a String | 移除 |
Replace/Overlay | Searches a String and replaces one String with another | 替换 |
Chomp/Chop | removes the last part of a String | 删除字符串的最后一部分 |
AppendIfMissing/PrependIfMissing | appends a suffix to the start/end of the String if not present | 前缀后缀 |
LeftPad/RightPad/Center/Repeat | pads a String | 填充字符串 |
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize | changes the case of a String | 大写/小写/交换大小写/大写/取消大写 |
CountMatches | counts the number of occurrences of one String in another | 统计匹配 |
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable | checks the characters in a String | 是否字母/数字/空格/Ascii |
DefaultString | protects against a null input String | 防止空输入字符串 |
Rotate | rotate (circular shift) a String | 旋转(循环移位)一个字符串 |
Reverse/ReverseDelimited | reverses a String | 反转字符串 |
Abbreviate | abbreviates a string using ellipses or another given String | 使用省略号或另一个给定的字符串缩写字符串 |
Difference | compares Strings and reports on their differences | 比较字符串并报告它们的差异 |
LevenshteinDistance | the number of changes needed to change one String into another | 将一个字符串更改为另一个字符串所需的更改次数 |
java的StringUtils.isBlank和StringUtils.isEmpty方法区别(org.apache.commons.lang3.StringUtils)相关推荐
- org.apache.commons.lang3.StringUtils 的相关用法
一.jar包下载 commons-lang3-3.1.jar java 开发工具commons-lang3-3.0 jar包,有org.apache.commons.lang3.StringUtils ...
- Java的org.apache.commons.lang3.StringUtils
Java的org.apache.commons.lang3.StringUtils 1.用途 StringUtils提供了对String 类型的常用操作方法(如判空等).StringUtils中的方法 ...
- Java中字符串工具类继承org.apache.commons.lang3.StringUtils类代码
场景 转换为字节数组:是否包含字符串:替换掉HTML标签方法:替换为手机识别的HTML,去掉样式及属性,保留回车: 缩略字符串(不区分中英文字符):转换为Double类型:转换为Float类型:转换为 ...
- Java基础学习总结(125)——org.apache.commons.lang3.StringUtils类方法详解
org.apache.commons.lang3.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的 ...
- 错误: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
做项目的时候,实现图片异步上传并返回json数据,但是图片上传成功,json数据没有返回,报错: java.lang.ClassNotFoundException: org.apache.commo ...
- hive执行drop卡死一例:java.lang.NoSuchMethodError: org.apache.commons.lang3.StringUtils.isAnyBlank
环境: 组件 版本 Hadoop 3.1.2 Hive 2.3.4 故障复现操作: hive中尝试drop table卡死,然后去hadoop的yarn界面发现如下报错 完整报错: org.apach ...
- Apache commons lang3 StringUtils工具类
Apache commons lang3 StringUtils工具类 Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常 ...
- 工具类org.apache.commons.lang3.StringUtils
sEmpty 和 isBlank 的区别你知道吗?也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道还有isAnyEmpty/isNoneEmpty/is ...
- org.apache.commons.lang3.StringUtils.isNotBlank和isEmpty方法
今天做项目的时候遇到一个小问题,检验字段不为空后,对其切割,用了isEmpty判断,报了空指针异常,最后发现库表里该字段为长度不为0空白字符串,这里isEmpty判断不了,下面列出来 StringUt ...
最新文章
- 转:Oracle物理文件
- 如何在Ubuntu 16.04中创建GIF动图
- web.config文件访问物理路径_计算机操作系统学习笔记(五):文件管理
- C++基础算法学习——熄灯问题
- 用户领域 API 监控和代码注入检测
- 值得收藏!9个最佳SSD状态监控及性能优化工具
- UML建模与软件工程
- Python熵权法确定权重
- Python的运行加速:C究竟比python快在哪
- C++线程编程-内存顺序
- 前端第一章:1.C/S架构、B/S架构简介、互联网的发明、渲染的解释、W3C万维网联盟、网页的结构
- 5天拿到华为Java岗offer
- STM32单片机开发实例 基于STM32单片机的智能行李箱
- 矩阵分析:广义逆矩阵,{1}逆,MP逆,D逆
- ubuntu安装pinta(图片编辑器)
- 此前小编为大家介绍了女人吃鸡蛋的好处,想必大家对鸡蛋这种蛋类有了更多的了解。今天小编为大家介绍另一种蛋类——鸭蛋。鸭蛋又名鸭卵,是人们经常食用的一种蛋类食品,与鸡蛋营养相当,吃它的好处众多。那么女人吃
- 附录2-PS基本操作
- window.print()打印时设置背景色
- 算法导论—AC自动机
- 嵌入式数据库SQLite
热门文章
- 9003软件工程_期末_李振宏老师
- 向Vector类中添加不同类型的元素,并输出Vector元素
- Unity 鼠标悬置在物体上,标签显示 + 移出物体,标签消失
- 南京大学大气科学学院王体健教授到访云创
- ADAS仿真测试-基于原始数据流的雷达感知测试
- 计算机联锁控制系统的软件应具备信号操作功能,计算机联锁技术学习包
- Android 应用(3)——Android10.0去掉主界面Launcher3程序
- Android 虚拟导航键适配
- 董明珠直播从翻车到3小时卖3亿是其半生写照:极度渴望成功,愿付非凡代价
- 发第一篇SCI有哪些技巧?