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

代码中 isEmpty 和 isBlank 的区别

时间:2024-09-22 17:55:04浏览次数:3  
标签:false 代码 StringUtils isEmpty CharSequence return null true 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 trueif 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 trueif 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 trueif 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 trueif 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 trueif 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 trueif 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

图片

图片

标签:false,代码,StringUtils,isEmpty,CharSequence,return,null,true,isBlank
From: https://blog.csdn.net/java_121388/article/details/142440502

相关文章

  • 源代码编译安装LAMP
    一、简介源代码编译安装LAMP:源代码:指的是软件的原始代码,是软件编写和编译的基础。编译:将源代码转换成机器可执行的二进制代码的过程。安装:将编译后的软件部署到服务器上并配置好,使其能够运行。源代码编译安装LAMP的特点:定制性:可以从源代码开始,根据服务器的具体需求进行编译......
  • "放开那代码让我来!"——Cursor帮你写代码的奇妙之旅
    让我们开门见山:编程很酷,但也很折磨人。那些长时间盯着屏幕,debug无休止的日子,只有程序员懂得它的酸爽。而就在这个编程焦虑的世界中,Cursor横空出世,带着一系列魔法功能,如同你手中的一根智能魔杖,让写代码变得像煮速冻面一样简单。Cursor,一款基于AI的编程助手,号称可以从自然语言生成......
  • 有服务器后怎么测试自己的写的代码
    后端程序员,如果想要测试自己写的代码肯定是需要一个工具的,这边我推荐的是广受好评的测试工具:ApiFox这款测试工具不仅美观功能还非常的强大,今天保姆级教学,如果有自己服务器的情况下该怎么去安装测试!!!该篇文章是有了服务器后的教学,如果需要服务器,可以看我下面链接的文章,阿里......
  • PTA L1-064 估值一亿的AI核心代码
    L1-064估值一亿的AI核心代码(20分)以上图片来自新浪微博。本题要求你实现一个稍微更值钱一点的AI英文问答程序,规则是:无论用户说什么,首先把对方说的话在一行中原样打印出来;消除原文中多余空格:把相邻单词间的多个空格换成1个空格,把行首尾的空格全部删掉,把标点符号前面的空......
  • Tarjan算法及其应用 总结+详细讲解+详细代码注释
    \(\text{Tarjan}\)1.引入概念1.强连通分量1.定义在有向图\(G\)中,强连通分量就是满足以下条件的\(G\)的子图:从任意一点出发,都有到达另一个点的路径。不存在一个或者几个点,使得这个子图加入这些点后,这个子图还满足上一个性质。为什么要限定”在有向图中“而不是”在任......
  • c#代码介绍23种设计模式_03工厂模式
    目录1.工厂方法模式之所以可以解决简单工厂的模式2.使用工厂方法实现的系统,如果系统需要添加新产品时3.从UML图来看4、实现思路1.工厂方法模式之所以可以解决简单工厂的模式是因为它的实现把具体产品的创建推迟到子类中,此时工厂类不再负责所有产品的创建,而只是给出......
  • 机器翻译之seq2seq训练、预测、评估代码
    目录1.seq2seq训练代码2.预测代码  3.评估代码 4.知识点个人理解 1.seq2seq训练代码seq2seq的训练代码:pytorch中训练代码一般都相同类似#将无效的序列数据都变成0(屏蔽无效内容的部分)defsequence_mask(X,valid_len,value=0):"""valid_len:有效序......
  • 听说ChatGPT o1推理模型即将问世,传统问答系统是否还有存在的必要?毕业设计:基于知识图谱
     OpenAI隆重推出全新一代的o1模型,该模型在多个领域展现出了非凡的能力,标志着人工智能技术的又一次飞跃。该模型专门解决比此前的科学、代码和数学模型能做到的更难的问题,实现复杂推理。那来看看并体验以下我们传统的问答系统的设计流程和具体面貌吧!!!1.1系统架构设计1.1.1......
  • 推荐一个很酷的脚本工具,几行代码,就能编写有用的 shell 脚本,月猛增 7.4 K Star太牛逼了
     今天给大家介绍的是gum,它是一个很酷的脚本工具。项目介绍gum是一个很棒的脚本工具,提供了高度可配置,随时可用的实用程序,只需几行代码,就能编写有用的shell脚本。让我们构建一个简单的脚本来创建提交。由下面的代码开始:#!/bin/sh询问gumchoose的提交类型:gum ch......
  • 【代码随想录Day24】回溯算法Part03
    93.复原IP地址题目链接/文章讲解:代码随想录视频讲解:回溯算法如何分割字符串并判断是合法IP?|LeetCode:93.复原IP地址_哔哩哔哩_bilibiliclassSolution{List<String>result=newArrayList<>();LinkedList<String>path=newLinkedList<>();publicL......