isEmpty 和 isBlank 请别乱用了,小心把服务器搞崩!

article/2025/10/15 4:22:05

大家好,我是老赵!

大家好,我是程序汪,开发中经常有些小细节容易忽略,这些小细节往往容易导致代码缺陷,今天分享一波工具类的小细节

也许你两个都不知道,也许你除了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

方法名英文解释中文解释
IsEmpty/IsBlankchecks if a String contains text检查字符串是否包含文本
Trim/Stripremoves leading and trailing whitespace删除前导和尾随空格
Equals/Comparecompares two strings null-safe比较两个字符串是否为null安全的
startsWithcheck if a String starts with a prefix null-safe检查字符串是否以前缀null安全开头
endsWithcheck if a String ends with a suffix null-safe检查字符串是否以后缀null安全结尾
IndexOf/LastIndexOf/Containsnull-safe index-of checks包含空安全索引检查
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyButindex-of any of a set of Strings任意一组字符串的索引
ContainsOnly/ContainsNone/ContainsAnydoes String contains only/none/any of these characters字符串是否仅包含/无/这些字符中的任何一个
Substring/Left/Right/Midnull-safe substring extractions字符串安全提取
SubstringBefore/SubstringAfter/SubstringBetweensubstring extraction relative to other strings -相对其他字符串的字符串提取
Split/Joinsplits a String into an array of substrings and vice versa将字符串拆分为子字符串数组,反之亦然
Remove/Deleteremoves part of a String -删除字符串的一部分
Replace/OverlaySearches a String and replaces one String with another搜索字符串,然后用另一个字符串替换
Chomp/Chopremoves the last part of a String删除字符串的最后一部分
AppendIfMissingappends a suffix to the end of the String if not present如果不存在后缀,则在字符串的末尾附加一个后缀
PrependIfMissingprepends a prefix to the start of the String if not present如果不存在前缀,则在字符串的开头添加前缀
LeftPad/RightPad/Center/Repeatpads a String填充字符串
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalizechanges the case of a String更改字符串的大小写
CountMatchescounts the number of occurrences of one String in another计算一个字符串在另一个字符串中出现的次数
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintablechecks the characters in a String检查字符串中的字符
DefaultStringprotects against a null input String防止输入字符串为空
Rotaterotate (circular shift) a String旋转(循环移位)字符串
Reverse/ReverseDelimitedreverses a String -反转字符串
Abbreviateabbreviates a string using ellipsis or another given String使用省略号或另一个给定的String缩写一个字符串
Differencecompares Strings and reports on their differences比较字符串并报告其差异
LevenshteinDistancethe number of changes needed to change one String into another将一个String转换为另一个String所需的更改次数

来源:https://sourl.cn/dRpJ6b

 

精彩推荐

1.求求你别再自己瞎写工具类了,Spring Boot 内置工具类涵盖了所有操作!
2.新来的大佬一个注解搞定 Spring Boot 接口恶意刷新和暴力请求!又学会一招!
3.这是我见过写得最烂的Controller层代码,没有之一!
4.3年工作经验,多线程间的5种通信方式一个都答不上来,你敢信?
5.IDEA 神级插件!效率提升 50 倍!
6.懵了!有了HTTP,为什么还要RPC?
7.再见,Java泛型 T、E、K、V、?。通用泛型要来了,非常劲爆!

36d878ea42ea4859207ba885847e3b89.png


http://chatgpt.dhexx.cn/article/UTDTwEOd.shtml

相关文章

java中isblank 方法_StringUtils中的isEmpty、isNotEmpty、isBlank和isNotBlank的区别详解

一、StringUtils中的isEmpty方法 1、StringUtils中的isEmpty方法中的源码如下: 注:由源码可知(判断某字符串是否为空,为空的标准是str==null或str.length()==0) 2、StringUtils中的isEmpty方法示例,如下代码 package com.rf.designPatterns.singleton; import org.apache.c…

isEmpty和isBlank的区别

isEmpty和isBlank的区别在于 isEmpty仅仅是判断空和长度为0字符串 isBlank判断的是空&#xff0c;长度为0&#xff0c;空白字符&#xff08;包括空格&#xff0c;制表符\t&#xff0c;换行符\n&#xff0c;换页符\f&#xff0c;回车\r&#xff09;组成的字符串。 比如&#…

Android studio模拟短信支付功能

需求&#xff1a; 模拟短信支付功能。创建两个Activity&#xff0c;功能分别为支付首页和短信验证Activity。用户在支付首页点击支付按钮&#xff0c;使用startActivityForResult()方法打开一个新的Activity&#xff0c;在短信验证Activity中模拟输入短信验证码&#xff0c;并…

沙箱-简单实现支付宝网页支付功能

1.在实现前我们得先了解支付宝支付功能-他是需要一个真实的软件 但我们没有真实的应用软件&#xff0c;所以支付宝给我们提供了一个沙箱的功能。沙箱就是一个虚拟的假的应用软件。了解沙箱可以看支付宝文档中心。----沙箱环境 &#xff5c; 开放平台 2.我们创建自己的沙箱账号…

uniapp实现支付功能

uniapp实现支付功能 详细参考&#xff1a; https://gitee.com/copperpeas/uniapp-payment uniapp-payment 介绍 uniapp支付 微信支付流程 测试接入的是uniapp官方预下单接口 APP应用首先去微信等开发平台申请开通支付&#xff0c;部分支付渠道需要配置支付目录&#xff0c;授…

微信小程序支付功能的实现

前言 最近需要在微信小程序中用到在线支付功能&#xff0c;于是看了一下官方的文档&#xff0c;发现要在小程序里实现微信支付还是很方便的&#xff0c;如果你以前开发过服务号下的微信支付&#xff0c;那么你会发现其实小程序里的微信支付和服务号里的开发过程如出一辙&#…

微信小程序前端微信支付功能 支付流程

小程序支付&#xff08;纯前端&#xff09; 前提&#xff08;实现微信小程序支付功能需要appid主体为企业&#xff0c;以及开通了微信商家服务的小程序才能实现&#xff0c;个人小程序是无法实现这个功能的&#xff09; 前期准备&#xff1a; 1.开通了微信支付&#xff0c;并…

微信小程序接入支付功能并实现支付

随着微信小程序越来越广泛的应用,现在小程序几乎无所不能(绝对啦,哈哈),那么就会有很多微信小程序需要有支付的需求,那么该文章将带领大家走一遍如何实现微信小程序的支付功能. 第一步,微信小程序管理后台 -> 微信支付->接入微信支付 及关联(设置)商户信息 如果是第一次…

软件测试之“支付功能”测试

测试思维 要分析测试点之前&#xff0c;我们先来梳理一下测试思维。总结来说&#xff0c;任何事物的测试思路都可以总结如下&#xff1a; 第一步&#xff1a;梳理产品的核心业务流程&#xff1a;明白这是个什么项目&#xff0c;实现了什么业务&#xff0c;以及是怎么实现的&a…

一步步教你如何在SpringBoot项目中引入支付功能

听说微信搜索《Java鱼仔》会变更强哦&#xff01; 本文收录于JavaStarter &#xff0c;里面有我完整的Java系列文章&#xff0c;学习或面试都可以看看哦 &#xff08;一&#xff09;引言 支付功能如今已经成为一个需要盈利的网站的基本功能了&#xff0c;如今的网站如果想要做…

支付功能怎么测试?

跳槽高峰期&#xff0c;作为测试&#xff0c;不管是面试还是笔试&#xff0c;必然要被考验到的就是”测试思维“。在面试中就是体现在如下面试题中&#xff1a; “说说你项目中的 xx 模块你是如何测试的&#xff1f;” “给你一个购物车&#xff0c;你要怎么测试&#xff1f;”…

面试题:支付功能怎么测试?如何回答?

文章末尾给大家准备了大量福利 前言 九月了&#xff0c;有很多的小伙伴已经全面武装好准备找工作了&#xff0c;九月和十月是黄金期——俗称”金九银十“。那么&#xff0c;作为测试&#xff0c;不管是面试还是笔试&#xff0c;必然要被考验到的就是”测试思维“。在面试中就…

支付功能测试用例(参考微信平台)

支付功能测试用例xmind&#xff0c;功能点以支付方式&#xff0c;支付手段&#xff0c;支付金额划分的 支付方式&#xff1a; 1.余额&#xff08;零钱&#xff09;支付 2.储蓄卡支付 3.第三方支付微信&#xff0c;支付宝&#xff0c;京东、百度、&#xff09; 4.信用卡支付…

vue项目支付功能

目录 1.支付宝方式&#xff1a; 代码&#xff1a; ​ 2.微信支付 二维码展示代码&#xff1a; ​请求后端的支付二维码接口 1.支付宝方式&#xff1a; 支付宝方式&#xff1a;点击支付宝支付, 调用后台接口(携带订单号)&#xff0c;后台返回一个form表单(HTML字符串结构)&am…

在线支付功能实现代码

我们都知道&#xff0c;在现在的网站中&#xff0c;基本上都会有支付功能&#xff0c;在线支付作为一个潮流已是现代化网站的必备功能模块&#xff0c;那么几天我就分享一下如果来做这个在线支付功能。 在线支付一般来说有两种实现方式&#xff0c;一种是调用各个银行提供的接口…

Java实现微信支付功能

微信实现支付功能与支付宝实现支付功能是相似的&#xff0c;接入前的准备工作&#xff0c;包括申请APPID、申请mchid、绑定APPID及mchid、配置API key、下载并配置商户证书等&#xff0c;具体可查看微信支付文档 接入前准备-APP支付 | 微信支付商户平台文档中心 (qq.com)正在上…

Java开发支付宝支付功能

之前做开发过程中&#xff0c;没有接触过支付相关的功能&#xff0c;最近做了一个支付相关功能的开发&#xff0c;包括支付宝和微信支付&#xff0c;为了避免以后忘记相关的流程&#xff0c;记录一下这次的开发经验&#xff0c;这里先介绍一下支付宝相关的开发。 首先在进行jav…

支付宝支付功能实现

支付宝支付功能 1、电脑网站支付&#xff0c;手机网站支付&#xff0c;app支付1.1、异步通知介绍1.2、API和请求示例介绍 2、当面付3、小程序支付接入4、代码完整代码 支付宝开发文档中心 注意&#xff1a;个人无法使用此功能&#xff0c;因为个人申请使用是不会通过的 1、电脑…

支付功能

Django rest framework之支付功能 一.支付宝支付 1.进入蚂蚁金服开放平台&#xff08;查看api&#xff09;&#xff1a; 1.1在正式生产环境中需要创建应用&#xff08;需审核&#xff09;&#xff1a; 1.2沙箱环境&#xff08;测试&#xff09;&#xff1a; 可以在文档中查看对…

springboot实现支付宝支付功能

支付系统中容易出现的问题 1&#xff0c;用户在页面下订单后&#xff0c;价格被篡改&#xff1b; 解决方案&#xff1a;通过后端计算订单的总金额 2&#xff0c;订单重复处理。用户支付成功后&#xff0c;支付宝会短时间内多次调用我们的回调接口&#xff0c;如果出现网络波动…