首页 > 其他分享 >isEmpty 和 isBlank 的区别

isEmpty 和 isBlank 的区别

时间:2023-08-06 20:32:31浏览次数:35  
标签:false 区别 isBlank isEmpty CharSequence return null true StringUtils

isEmpty 和 isBlank 的区别_css


背景:

"isEmpty" 和 "isBlank" 是两个常见的字符串操作方法,用于检查字符串是否为空或空白。它们在不同编程语言和库中可能有些许差异,我将为您提供通用的理解。     


       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);
}


标签:false,区别,isBlank,isEmpty,CharSequence,return,null,true,StringUtils
From: https://blog.51cto.com/u_13119980/6986578

相关文章

  • APB的版本区别
    APB的版本区别https://blog.csdn.net/qq_40571921/article/details/122853718https://github.com/baimengwei/yuu_apb/blob/master/src/sv/yuu_apb_common/yuu_apb_agent_config.sv#L19APB3认为是B版本。APB3support,includeWaitstatesandErrorreportingAPB2和APB3的......
  • Python文件读取方法:read()、readline()和readlines()的区别
    在Python中,读取文件是一项常见的任务。Python提供了多种方法来读取文件内容,其中包括read()、readline()和readlines()方法。本文将介绍这些方法的区别和使用场景。read()read()方法用于一次性读取整个文件的内容,并将其作为一个字符串返回。语法如下:file_object.read()优点:读取整个......
  • 快照和备份的区别
    快照和备份是在计算机领域常见的两种数据保护手段,它们有一些区别:快照(Snapshot):快照是一种在特定时间点对系统或数据进行的镜像副本。快照通常是通过记录系统的当前状态或数据块的差异来创建的,而不是复制整个数据集。快照是在存储层面上进行的,例如虚拟机或存储设备层面的快照。快照可......
  • Lua ipairs和pairs的区别
    在Lua语言中,ipairs和pairs都可以应用于对表和数组的遍历,但它们之间有什么区别呢?首先,我们要知道Lua中的表可以以数字或字符串作为表的键key,但用数字作为key时,可以称为索引id。当以连续不间断的数字索引作为表的key时,这种表就可以称为数组。ipairs就主要应用于数组中,会从1开始有序......
  • "静态方法和实例方法" 这两种函数调用的区别
    来看两段代码第一段:publicclassRegexDemo{publicstaticvoidmain(String[]args){func();}privatestaticvoidfunc(){Stringinput="123456";booleanmatches=input.matches("\\d+");Syste......
  • Vue进阶(幺肆贰):CSS-静态定位,相对定位,绝对定位,固定定位的用法和区别详解
    (文章目录)一、前言CSS提供了三种基本的定位机制:普通流、浮动和固定定位;通过这三种方式可实现页面的排版布局。二、普通流普通流中元素的位置由元素在(X)HTML中的位置决定:块级元素独自占一行,在文本流中从上到下一个接一个地排列;行内元素在一行中并排排列,遇到父元素的......
  • 关于union和合并单元格的区别
    选择与单元格A1中的值相等的所有单元格SubselectSameCells()DimgoalRangeAsRange,indexCellAsRangeSetgoalRange=Range("A1")ForEachindexCellInRange("A1:B5")IfindexCell.Value=Range("A1").ValueThen......
  • 深入理解线程与进程:概念、特点与区别,附带代码演示
    当今计算机系统中,线程(Thread)和进程(Process)是并发编程中的关键概念。它们对于提高程序的效率和性能至关重要。本篇博客将详细介绍线程和进程的概念、特点以及它们之间的区别,同时通过代码演示来加深理解。1.线程1.1概念线程是操作系统能够进行运算调度的最小单位。一个进程可以包含......
  • 硬链接和软链接的区别
    硬链接和软链接的区别1、本质不同硬链接:同一个文件,多个名称。​软链接:不同的文件。2、跨分区硬链接:不支持跨分区。软链接:支持跨分区。3、目录硬链接:不支持对目录创建。​软链接:支持对目录创建。4、相互关系​硬链接:删除某一个硬链接,另一个硬链接不影响使用。​软链......
  • cookie和localStorage和sessionStorage的区别
    cookie和localStorage和sessionStorage的区别下面从几个方向区分一下cookie,localStorage,sessionStorage的区别生命周期:cookie:可设置失效时间,否则默认为关闭浏览器后失效。localStorage:除非被手动清除,否则永久保存。sessionStorage:仅在当前网页会话下有效,关闭页面或关......