java的StringUtils.isBlank和StringUtils.isEmpty方法区别(org.apache.commons.lang3.StringUtils)

本文详细介绍了Apache Commons Lang库中StringUtils类的isEmpty、isBlank、isNotBlank以及相关方法的用法,如isAnyBlank和isNoneBlank。这些方法用于检查字符串是否为空、仅包含空白字符等,对于字符串处理非常实用。了解这些方法可以帮助优化代码中的字符串检查逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

估计很多朋友跟我一样,平时也不会特别去注意究竟用isBlank还是isEmpty去判断空字符串,但是大部分场景优先使用isBlank就对了。

  • isEmpty是否为空,只有当==null或者==""才为空
  • isBlank是否为真空,==null==""以及各种长度的空格==" "都为空

而且除了isEmpty/isNotEmpty/isNotBlank/isBlank外,其实还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank让我们一起来研究一下org.apache.commons.lang3.StringUtils这个工具类吧。

isBank()

是否为真空值(空格或者空值)

- StringUtils.isBlank(null) = true
- StringUtils.isBlank("") = true
- StringUtils.isBlank(" ") = true
- StringUtils.isBlank("                ") = true
- StringUtils.isBlank("moshow") = false
- StringUtils.isBlank("   moshow  ") = 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, " ") = true
StringUtils.isAnyBlank("", " ") = true
 /**
 * <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, " ") = false
StringUtils.isNoneBlank("", " ") = false
StringUtils.isNoneBlank("moshow", " moshow ") = true

关于其他方法其实都是以此类推,知道这几个,就可以类推对应的is*Empty等等鞥,这里就不做重复展示。

/**
 * <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

方法 (整理 by https://zhengkai.blog.csdn.net/说明EN说明CN
IsEmpty/IsBlankchecks if a String contains text判断是否包含文本
Trim/Stripremoves leading and trailing whitespace删除多余空格
Equals/Comparecompares two strings in a nullsafe manner
startsWith /endsWithcheck if a String starts/ends with a prefix判断是什么开头/结尾
IndexOf/LastIndexOf/Containscontain a string and return the index判断字符串在string中的位置,判断是否包含
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyButindex of any of a set of Strings如果包含某几个字符串就返回
ContainsOnly/ContainsNone/ContainsAnychecks if String contains only/none/any of these characters
Substring/Left/Right/Mid/SubstringBefore/SubstringAfter/SubstringBetweensafe substring extractions截取字符串方法
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删除字符串的最后一部分
AppendIfMissing/PrependIfMissingappends a suffix to the start/end 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是否字母/数字/空格/Ascii
DefaultStringprotects against a null input String防止空输入字符串
Rotaterotate (circular shift) a String旋转(循环移位)一个字符串
Reverse/ReverseDelimitedreverses a String反转字符串
Abbreviateabbreviates a string using ellipses or another given String使用省略号或另一个给定的字符串缩写字符串
Differencecompares Strings and reports on their differences比较字符串并报告它们的差异
LevenshteinDistancethe number of changes needed to change one String into another将一个字符串更改为另一个字符串所需的更改次数
### 下载 Popper.min.js 文件的方法 对于希望获取 `popper.min.js` 的开发者来说,可以通过多种方式来实现这一目标。通常情况下,推荐通过官方渠道或可靠的分发网络 (CDN) 来获得最新的稳定版文件。 #### 使用 CDN 获取 Popper.min.js 最简单的方式之一是从流行的 CDN 中加载所需的 JavaScript 库。这不仅简化了集成过程,还可能提高性能,因为许多用户已经缓存了来自这些服务提供商的内容。例如: ```html <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2/dist/umd/popper.min.js"></script> ``` 这种方式不需要手动下载文件到本地服务器;只需将上述 `<script>` 标签添加至 HTML 文档中的适当位置即可立即使用 Popper 功能[^1]。 #### 从 npm 或 yarn 安装 如果项目采用模块化构建工具链,则可以直接利用包管理器如 npm 或 Yarn 进行安装。命令如下所示: ```bash npm install @popperjs/core # 或者 yarn add @popperjs/core ``` 之后可以根据具体需求引入特定功能模块,而不是整个库,从而减少打包后的体积并优化加载速度[^2]。 #### 访问 GitHub 发布页面下载压缩包 另一种方法是访问 Popper.js 的 [GitHub Releases](https://github.com/popperjs/popper-core/releases) 页面,在这里可以选择不同版本的 tarball 或 zip 归档进行下载解压操作。这种方法适合那些偏好离线工作环境或是想要定制编译选项的人群[^3]。 #### 手动克隆仓库 最后一种较为少见但也可行的办法便是直接克隆完整的 Git 存储库副本。这样可以获得开发分支以及历史记录等更多信息,适用于贡献代码或者深入学习内部机制的情况。 ```bash git clone https://github.com/popperjs/popper-core.git cd popper-core ``` 完成以上任一途径后便能成功取得所需版本的 Popper.min.js 文件,并将其应用于个人项目之中[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值