java中isEmpty和isBlank的区别小结

 更新时间:2023年09月01日 10:23:14   作者:小田资料库  
Java中的isEmpty和isBlank都是用来判断字符串是否为空的方法,但在不同的情况下有所区别,具有一定的参考价值,感兴趣的可以了解一下

干了3年java,代码中 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) {

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

到此这篇关于java中isEmpty和isBlank的区别小结的文章就介绍到这了,更多相关java isEmpty isBlank内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Java springboot Mongodb增删改查代码实例

    Java springboot Mongodb增删改查代码实例

    这篇文章主要介绍了Java springboot Mongodb增删改查代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Spring Bean生命周期源码原理图解

    Spring Bean生命周期源码原理图解

    这篇文章主要介绍了Spring Bean生命周期源码原理图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • Springboot日志开启SLF4J过程解析

    Springboot日志开启SLF4J过程解析

    这篇文章主要介绍了Springboot日志开启SLF4J过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • 使用JWT创建解析令牌及RSA非对称加密详解

    使用JWT创建解析令牌及RSA非对称加密详解

    这篇文章主要介绍了JWT创建解析令牌及RSA非对称加密详解,JWT是JSON Web Token的缩写,即JSON Web令牌,是一种自包含令牌,一种情况是webapi,类似之前的阿里云播放凭证的功能,另一种情况是多web服务器下实现无状态分布式身份验证,需要的朋友可以参考下
    2023-11-11
  • Vue + springboot实现拼图人机验证功能

    Vue + springboot实现拼图人机验证功能

    本文介绍了如何使用Vue和Spring Boot实现拼图人机验证功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2026-01-01
  • SpringBoot项目修改访问端口和访问路径的方法

    SpringBoot项目修改访问端口和访问路径的方法

    这篇文章主要介绍了SpringBoot项目修改访问端口和访问路径的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • java使用Runtime.getRuntime().exec调用外部程序

    java使用Runtime.getRuntime().exec调用外部程序

    Runtime.getRuntime().exec一般用于调用外部可执行程序或系统命令,并重定向外部程序的标准输入,准输出和标准错误到缓冲池,下面我们来看看Java如何使用它进行调用外部程序吧
    2025-06-06
  • 详解mybatis多对一关联查询的方式

    详解mybatis多对一关联查询的方式

    这篇文章主要给大家介绍了关于mybatis多对一关联查询的相关资料,文中将关联方式以及配置方式介绍的很详细,需要的朋友可以参考下
    2021-06-06
  • SpringBoot手写starter全过程

    SpringBoot手写starter全过程

    本文主要介绍了如何手动编写SpringBoot的starter,包括创建项目、配置自动配置类、编写Properties文件、配置spring.factories文件等步骤,并通过测试项目验证其功能,同时,还讨论了如何实现开关控制等功能
    2026-05-05
  • Java中的JVM内存分析与故障排查指南

    Java中的JVM内存分析与故障排查指南

    Java虚拟机(JVM)是Java应用的运行时环境,其内存管理机制直接影响着应用的性能和稳定性,本文将介绍JVM内存分析的基本方法,重点介绍如何使用jmap、jhat和VisualVM等工具进行内存分析,并探讨常见的内存泄漏排查方法,需要的朋友可以参考下
    2025-11-11

最新评论