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将一个字符串更改为另一个字符串所需的更改次数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值