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

 

        

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/93751.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1.(python数模)单函数读取常用文件

Python单函数读取常用文件 代码如下&#xff1a; import pandas as pd# 读取数据文件 def readDataFile(readPath): # readPath: 数据文件的地址和文件名try:if (readPath[-4:] ".csv"):dfFile pd.read_csv(readPath, header0, sep",") # 间隔符为逗…

DBO优化SVM的电力负荷预测,附MATLAB代码

今天为大家带来一期基于DBO-SVM的电力负荷预测。 原理详解 文章对支持向量机(SVM)的两个参数进行优化&#xff0c;分别是&#xff1a;惩罚系数c和 gamma。 其中&#xff0c;惩罚系数c表示对误差的宽容度。c越高&#xff0c;说明越不能容忍出现误差,容易过拟合。c越小&#xff0…

按照json文件的值复制图片

按照json文件的值复制图片 文件格式处理当前JSON代码封装增加批处理 文件格式 0是不挑选&#xff0c;1是挑选 处理当前JSON # coding: utf-8 from PIL import Image, ImageDraw, ImageFont import os import shutil import cv2 as cv import numpy as np import jsondef read…

linux定时删除服务器日志

不说废话。直接进入操作流程 linux 定时任务是用的crontab 查看 crontab是否启动 service crond statusdead 死的 启动crontab service crond start 再次查看状态 running 运转的 查看 crontab cat /etc/crontab 查看 crontab任务 crontab -l 编辑 crontab任务 crontab …

大数据专业毕业能从事什么工作

大数据从业领域很宽广&#xff0c;不管是科技领域还是食品产业&#xff0c;零售业等都是需要大数据人才进行大数据的处理&#xff0c;以提供更好的用户体验&#xff0c;优化库存降低成本预测需求。 大数据开发做什么&#xff1f; 大数据开发分两类&#xff0c;编写Hadoop、Spa…

Java中支持分库分表的框架/组件/中间件简介

文章目录 1 sharding-jdbc2 TSharding3 Atlas4 Cobar5 MyCAT6 TDDL7 Vitess 列举一些比较常见的&#xff0c;简单介绍一下&#xff1a; sharding-jdbc&#xff08;当当&#xff09; TSharding&#xff08;蘑菇街&#xff09; Atlas&#xff08;奇虎360&#xff09; Cobar&#…

Vue3 学习 组合式API setup语法糖 响应式 指令 DIFF(一)

文章目录 前言一、Composition Api二、setup语法糖三、响应式refreactive 四、其他一些关键点v-prev-oncev-memov-cloak 五、虚拟Dom五、diff算法 前言 本文用于记录学习Vue3的过程 一、Composition Api 我觉得首先VUE3最大的改变就是对于代码书写的改变&#xff0c;从原来选择…

五子棋游戏禁手算法的改进

五子棋游戏禁手算法的改进 五子棋最新的禁手规则&#xff1a; 1&#xff0e;黑棋禁手判负、白棋无禁手。黑棋禁手有“三三”&#xff08;包括“四三三”&#xff09;、“四四”&#xff08;包括“四四三”&#xff09;和“长连”。黑棋只能以“四三”取胜。 2&#xff0e;黑方…

面试被打脸,数据结构底层都不知道么--回去等通知吧

数据结构之常见的8种数据结构&#xff1a; -数组Array -链表 Linked List -堆 heap -栈 stack -队列 Queue -树 Tree -散列表 Hash -图 Graph 数据结构-链表篇 Linklist定义&#xff1a; -是一种线性表&#xff0c;并不会按线性的顺序存储数据&#xff0c;即逻辑上相邻…

一种改进多旋翼无人机动态仿真的模块化仿真环境研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

Maven之hibernate-validator 高版本问题

hibernate-validator 高版本问题 hibernate-validator 的高版本&#xff08;邮箱注解&#xff09;依赖于高版本的 el-api&#xff0c;tomcat 8 的 el-api 是 3.0&#xff0c;满足需要。但是 tomcat 7 的 el-api 只有 2.2&#xff0c;不满足其要求。 解决办法有 2 种&#xff…

2023.9.1 简单认识 JVM

目录 JVM 内存划分 本地方法栈 虚拟机栈 程序计数器 堆区 元数据区 JVM 类加载机制 加载 验证 准备 解析 初始化 类被加载的几种情况&#xff08;懒汉模式 ---> 只要被用到才会被加载&#xff09; 双亲委派模型 JVM 内存划分 JVM 是一个应用程序&#xff0c;在…