isEmpty 和 isBlank 的用法区别

2023-05-16

也许你两个都不知道,也许你除了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

方法名英文解释中文解释
IsEmpty/IsBlankchecks if a String contains text检查字符串是否包含文本
Trim/Stripremoves leading and trailing whitespace删除前导和尾随空格
Equals/Comparecompares two strings null-safe比较两个字符串是否为null安全的
startsWithcheck if a String starts with a prefix null-safe检查字符串是否以前缀null安全开头
endsWithcheck if a String ends with a suffix null-safe检查字符串是否以后缀null安全结尾
IndexOf/LastIndexOf/Containsnull-safe index-of checks包含空安全索引检查
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyButindex-of any of a set of Strings任意一组字符串的索引
ContainsOnly/ContainsNone/ContainsAnydoes String contains only/none/any of these characters字符串是否仅包含/无/这些字符中的任何一个
Substring/Left/Right/Midnull-safe substring extractions字符串安全提取
SubstringBefore/SubstringAfter/SubstringBetweensubstring extraction relative to other strings -相对其他字符串的字符串提取
Split/Joinsplits a String into an array of substrings and vice versa将字符串拆分为子字符串数组,反之亦然
Remove/Deleteremoves part of a String -删除字符串的一部分
Replace/OverlaySearches a String and replaces one String with another搜索字符串,然后用另一个字符串替换
Chomp/Chopremoves the last part of a String删除字符串的最后一部分
AppendIfMissingappends a suffix to the end of the String if not present如果不存在后缀,则在字符串的末尾附加一个后缀
PrependIfMissingprepends a prefix to the start of the String if not present如果不存在前缀,则在字符串的开头添加前缀
LeftPad/RightPad/Center/Repeatpads a String填充字符串
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalizechanges the case of a String更改字符串的大小写
CountMatchescounts the number of occurrences of one String in another计算一个字符串在另一个字符串中出现的次数
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintablechecks the characters in a String检查字符串中的字符
DefaultStringprotects against a null input String防止输入字符串为空
Rotaterotate (circular shift) a String旋转(循环移位)字符串
Reverse/ReverseDelimitedreverses a String -反转字符串
Abbreviateabbreviates a string using ellipsis or another given String使用省略号或另一个给定的String缩写一个字符串
Differencecompares Strings and reports on their differences比较字符串并报告其差异
LevenshteinDistancethe number of changes needed to change one String into another将一个String转换为另一个String所需的更改次数

  

  

  

  

  

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

isEmpty 和 isBlank 的用法区别 的相关文章

  • STL常用算法——排序算法

    文章目录 STL常用算法 排序算法1 sort 2 random shuffle 3 merge 4 reverse STL常用算法 排序算法 1 sort sort xff1a 对容器或普通数组中范围内的元素进行排序 xff0c 默认进行
  • STL常用算法——拷贝和替换算法

    文章目录 STL常用算法 拷贝和替换算法1 copy 2 replace 3 replace if 4 swap STL常用算法 拷贝和替换算法 1 copy copy 函数 xff1a 将源容器内指定范围的元素拷贝到目的容器中 函数原型
  • STL常用算法——算术生成算法和集合算法

    文章目录 STL常用算法 算术生成算法和集合算法1 算术生成算法1 1 accumulate 1 2 fill 和fill n 2 集合算法2 1 set intersection 2 2 set union 2 3 set differe
  • CompletableFuture的使用

    文章目录 1 Future2 CompletableFuture 并行 xff0c 并发 并发 xff1a 一个实体上 xff0c 多个任务有序执行 并行 xff1a 多个实体上 xff0c 多个任务同时执行 用户线程 用户线程是系统的工作
  • 将本地jar添加到Maven仓库

    一 将jar添加到本地仓库的做法 xff1a 以下面pom xml依赖的jar包为例 xff1a 实际项目中pom xml依赖写法 xff1a html view plain copy lt dependency gt lt groupId
  • nodejs如何实现Digest摘要认证?

    文章目录 1 前言2 原理3 过程4 node实现摘要认证5 前端如何Digest摘要登录认证 xff08 下面是海康的设备代码 xff09 1 前言 根据项目需求 xff0c 海康设备ISAPI协议需要摘要认证 xff0c 那么什么是摘要
  • HAL库中断方式进行串口通信

    目录 一 通过CubeMX配置项目 二 在keil配置代码 三 烧录运行 四 输出 五 总结 六 参考链接 一 通过CubeMX配置项目 二 在keil配置代码 main函数中的while循环里面添加传输代码 if flag 61 61 1
  • 串口UART

    目录 串口概念 串口rs232 数据格式 注意事项 总体结构图 代码verilog 接收模块 结构图 波形图 编辑 代码 verilog 发送模块 结构图 波形图 代码 verilog 串口rs485 串口概念 串口是异步 串行通信接口 x
  • vs code 无法打开任何文件/新建文件报错this.configurationService.getValue(…) || []).filter is not a function

    vs code 无法打开任何文件 新建文件报错this configurationService getValue filter is not a function 主要起因是在一台mac 电脑上登录了vs code的同步帐号 xff0c
  • ESP32环境搭建遇到的问题记录

    关于安信可AiThinkerIDE V1 0自带的esp idf xff08 v3 3 咨询淘宝上的技术 xff09 xff0c 是可以在该IDE上导入运行的 xff1b 但是我使用了最新的esp idf v4 2 xff0c 在IDE上却
  • SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤

    一 xff1a 使用场景 xff1a 假如你的项目 xff08 这里指的是手机客户端项目 xff09 的某个版本 xff08 例如1 0版本 xff09 已经完成开发 测试并已经上线了 xff0c 接下来接到新的需求 xff0c 新需求的开
  • 有关HTTP 401验证的那些事儿

    前段时间突然遇到有一个需求 xff1a 要求能够抓取到NVR上连接的摄像头设备列表 因为要的比较急 xff0c 而且我还没啃透海康SDK的文档 xff0c 所以只好考虑另辟蹊径 xff0c 用一些别的方法来达到目标咯 我们登录到NVR的we
  • LCD段码屏 真值表转换

    以lcd段码屏驱动芯片TM1621D为例子 typedef union struct uint8 t a 1 uint8 t b 1 uint8 t c 1 uint8 t d 1 uint8 t e 1 uint8 t f 1 uint8
  • 段码屏走线转换为真值表

    54117PIN1234567891011SEG1ABCDEG PM2EFG2ABCD3EFG COL3ABCD4EFG4ABCD1B 2A COL 3A 4A1ADEG 2BF 3BF 4BF1C 2CG 3CG 4CGPM 2DE 3D
  • 硬件电路,AD-DC电路中元器件的作用

    热敏电阻 xff1a 功率型NTC热敏电阻多用于电源抑制浪涌 1 在AC220V输入端串联热敏电阻 xff0c 在电路电源接通瞬间 xff0c 电路中会产生比正常工作时高出许多倍的浪涌电流 xff0c 而NTC热敏电阻器的初始阻值较大 xf
  • CentOS 7内核更换教程

    CentOS 7支持安装锐速的内核 xff1a 3 10 0 327 el7 x86 64 使用下面命令下载及更换内核 rpm ivh http xz wn789 com CentOSkernel kernel 3 10 0 229 1 2
  • 关于STM32 CAN 滤波器设置的记录

    滤波模式有以下两种 xff1a 屏蔽位模式 标识符列表模式 过滤器的位宽 xff1a 16位过滤器 32位过滤器 下面记录一下我做过测试的代码 代码说明 xff1a 这是CAN2的滤波器 xff0c stm32f107的两组CAN滤波器是共
  • 定时器判断串口接收结束

    void USART1 IRQHandler void 串口1中断服务程序 u8 Res if USART GetITStatus USART1 USART IT RXNE 61 RESET 接收中断 Res 61 USART Receiv
  • gcc编译时对'xxxx'未定义的引用问题

    这个主要的原因是gcc编译的时候 xff0c 各个文件依赖顺序的问题 在gcc编译的时候 xff0c 如果文件a依赖于文件b xff0c 那么编译的时候必须把a放前面 xff0c b放后面 例如 在main c中使用了temp xff0c
  • 【编译人生】跨平台程序设计BOOST库以及编译方案的选择

    boost库很方便 xff0c 不用说 xff0c 下面是编译方法 xff0c 以WINDOWS平台为例 1 在 boost解压缩文件路径下 xff08 可能不同版本的路径位置build有所不同 xff09 cd d tools build

随机推荐

  • CAN总线的标准帧和扩展帧

    CAN总线的标准帧和扩展帧主要决定帧ID的长度 xff0c 标准帧的帧ID长度是11位 xff0c 帧ID的范围是000 7FF 扩展帧的帧ID长度是29位 xff0c 帧ID的范围是0000 0000 1FFF FFFF CANopen帧
  • CAN扩展帧详解

    寻址方式
  • linux 发送get/post请求

    目录 get post 43 json get curl location request GET 39 http xxxx param1 61 2027xxxx 39 url参数中涉及特殊字符的参数部分 需要转义 例如 curl 34 h
  • ROS -PCL程序包建立和CMakelist.txt修改

    一 创建工作空间 wtj 64 wtj echo ROS PACKAGE PATH wtj 64 wtj mkdir p dev catkin ws src wtj 64 wtj cd dev catkin ws src wtj 64 wt
  • jetson nano 供电模式的切换或自定义供电模式

    前言 xff1a jetson nano 开发板在预设的10W MAXN 模式下需要用5v4A的DC供电 用5v2A的DC或者micro usb供电建议使用5W模式 供电不足会导致掉电关机 以下是学习jetson nano时 xff0c 对
  • 自动驾驶之——CAN总线简介

    自动驾驶技术之 无人驾驶中的CAN总线 CAN 是Controller AreaNetwork 的缩写 xff0c 中文名为控制器局域网络 xff0c 是ISO国际标准化的串行通信协议 xff0c 是一种用于实时应用的串行通讯协议总线 xf
  • CMake中find_package()查找指定版本的库,以Qt库多版本共存为例

    Qt安装了多个版本时 xff0c CMake中写的find package 到底找到的是哪个库 xff1f 例如 xff0c 我电脑安装了两个版本的Qt xff0c 一个是5 12 3另一个是5 14 2 此时我的CMake如何指定使用哪个
  • 游戏中常用的寻路算法(6):地图表示

    在本系列文档大部分内容中 xff0c 我都假设A 用于某种网格上 xff0c 其中的 节点 是一个个网格的位置 xff0c 边 是从某个网格位置出发的各个方向 然而 xff0c A 可用于任意图形 xff0c 不仅仅是网格 xff0c 有很
  • Redis 官方可视化工具

    RedisInsight 是一个直观高效的 Redis GUI 管理工具 xff0c 它可以对 Redis 的内存 连接数 命中率以及正常运行时间进行监控 xff0c 并且可以在界面上使用 CLI 和连接的 Redis 进行交互 xff08
  • 一个注解搞定接口返回数据脱敏

    下午惬意时光 xff0c 突然产品小姐姐走到我面前 xff0c 打断我短暂的摸鱼time xff0c 企图与我进行深入交流 xff0c 还好我早有防备没有闪 xff0c 打开瑞star的点单页面 xff0c 暗示没有一杯coffee解决不了
  • 系统架构性能问题诊断及优化思路

    01 系统性能问题分析流程 我们首先来分析下如果一个业务系统上线前没有性能问题 xff0c 而在上线后出现了比较严重的性能问题 xff0c 那么实际上潜在的场景主要来自于以下几个方面 业务出现大并发的访问 xff0c 导致出现性能瓶颈 上线
  • 在Redis分布式锁上,栽的8个跟头

    在分布式系统中 xff0c 由于 redis 分布式锁相对于更简单和高效 xff0c 成为了分布式锁的首先 xff0c 被我们用到了很多实际业务场景当中 但不是说用了 redis 分布式锁 xff0c 就可以高枕无忧了 xff0c 如果没有
  • 牢记16个有用的 SpringBoot 扩展接口

    1 背景 Spring的核心思想就是容器 xff0c 当容器refresh的时候 xff0c 外部看上去风平浪静 xff0c 其实内部则是一片惊涛骇浪 xff0c 汪洋一片 Springboot更是封装了Spring xff0c 遵循约定大
  • ZYNQ研究----(3)7100 裸跑LWIP协议栈

    硬件环境 xff1a 创龙TLZ7XH EVM开发板 软件环境 xff1a VIVADO 2017 4 1 调用ZYNQ核 查开发板原理图 xff0c MIO16 27为以太网接口52 53为MDIO接口 xff0c 配置如下 使能串口1
  • SQL优化 20 连击

    一 查询SQL尽量不要使用select xff0c 而是具体字段 1 反例 SELECT FROM user 2 正例 SELECT id username tel FROM user 3 理由 节省资源 减少网络开销 可能用到覆盖索引 x
  • 对外 API 接口,请把握这3 条原则,16 个小点

    对外API接口设计 安全性 1 创建appid appkey和appsecret 2 Token xff1a 令牌 xff08 过期失效 xff09 3 Post请求 4 客户端IP白名单 xff08 可选 xff09 5 单个接口针对IP
  • 40 个 SpringBoot 常用注解:让生产力爆表!

    64 RequestMapping 64 RequestMapping注解的主要用途是将Web请求与请求处理类中的方法进行映射 Spring MVC和Spring WebFlux都通过RquestMappingHandlerMapping和
  • 分页 + 模糊查询 有坑!

    前言 不知道你有没有使用过Mysql的like语句 xff0c 进行模糊查询 xff1f 不知道你有没有将查询结果 xff0c 进行分页处理 xff1f 模糊查询 xff0c 加上分页处理 xff0c 会有意想不到的坑 xff0c 不信我们
  • Spring Boot + Netty + WebSocket 实现消息推送

    关于Netty Netty 是一个利用 Java 的高级网络的能力 xff0c 隐藏其背后的复杂性而提供一个易于使用的 API 的客户端 服务器框架 Maven依赖 lt dependencies gt lt https mvnreposi
  • isEmpty 和 isBlank 的用法区别

    也许你两个都不知道 也许你除了isEmpty isNotEmpty isNotBlank isBlank外 并不知道还有isAnyEmpty isNoneEmpty isAnyBlank isNoneBlank的存在 come on 让我们