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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • 深入了解SpringBoot中的统一返回和统一异常处理

    深入了解SpringBoot中的统一返回和统一异常处理

    这篇文章主要为大家详细介绍了SpringBoot项目中常用的统一返回结果和统一异常处理,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2024-01-01
  • 详解高性能缓存Caffeine原理及实战

    详解高性能缓存Caffeine原理及实战

    Caffeine是基于Java 8开发的,提供了近乎最佳命中率的高性能本地缓存组件,Spring5开始不再支持Guava Cache,改为使用Caffeine。Caffeine提供的内存缓存使用参考Google guava的API
    2021-06-06
  • Java8新特性之类型注解_动力节点Java学院整理

    Java8新特性之类型注解_动力节点Java学院整理

    这篇文章主要介绍了Java8新特性之类型注解的相关资料,需要的朋友可以参考下
    2017-06-06
  • Spring boot集成redis lettuce代码实例

    Spring boot集成redis lettuce代码实例

    这篇文章主要介绍了Spring boot集成redis lettuce代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • springboot 参数格式校验操作

    springboot 参数格式校验操作

    这篇文章主要介绍了springboot 参数格式校验操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 教你快速搭建sona服务及idea使用sona的方法

    教你快速搭建sona服务及idea使用sona的方法

    Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,本文给大家分享搭建sona服务及idea使用sona的方法,感兴趣的朋友一起看看吧
    2021-06-06
  • Java回调函数原理实例与代理模式的区别讲解

    Java回调函数原理实例与代理模式的区别讲解

    今天小编就为大家分享一篇关于Java回调函数原理实例与代理模式的区别讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • Java基础之位运算知识总结

    Java基础之位运算知识总结

    最近接触到了java位运算,之前对位运算的了解仅仅停留在表现结果上,乘2除以2,对背后的原理并不了解,现在学习记录一下,需要的朋友可以参考下
    2021-05-05
  • Java中枚举的使用方法详解

    Java中枚举的使用方法详解

    这篇文章主要介绍了Java中枚举的使用方法详解,比如我们想声明一组季节的集合,那这里面最多有四种,即春夏秋冬,不允许有其他的季节,那为了实现这种限制,体现出季节是固定的四个对象,我们可以使用枚举,需要的朋友可以参考下
    2023-07-07
  • Maven 多模块父子工程的实现(含Spring Boot示例)

    Maven 多模块父子工程的实现(含Spring Boot示例)

    这篇文章主要介绍了Maven 多模块父子工程的实现(含Spring Boot示例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04

最新评论