isEmpty和isBlank的用法区别,至少一半的人答不上来

三年前,在之前公司上班时,碰到了一个工作三年的程序员,他居然没搞懂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) {{
        return !isAnyEmpty(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

图片

图片

图片

### Java 中 String 的 isEmpty isBlank 方法的区别Java 中,`isEmpty` `isBlank` 是用于判断字符串是否为空或空白的两种方法。尽管它们看起来功能相似,但实际上存在显著的区别。 #### 1. **isEmpty 方法** `isEmpty` 是 Java 中 `String` 类的一个方法,用于判断字符串是否为空。如果字符串长度为 0,则返回 `true`;否则返回 `false`[^1]。 ```java "".isEmpty(); // true " ".isEmpty(); // false ``` 从上述代码可以看出,`isEmpty` 只检查字符串的长度是否为 0,而关心字符串内容是否包含空格或其他字符[^2]。 #### 2. **isBlank 方法** `isBlank` 是 JDK 11 引入的新方法,用于判断字符串是否为空白字符串。如果字符串为 `null`、长度为 0 或仅包含空白字符(如空格、制表符、换行符等),则返回 `true`[^2]。 ```java "".isBlank(); // true " ".isBlank(); // true "\t\n".isBlank(); // true "abc".isBlank(); // false ``` 从代码示例中可以看到,`isBlank` 仅检查字符串长度,还检查字符串内容是否仅为空白字符[^3]。 #### 3. **主要区别** - **定义范围**: - `isEmpty` 仅判断字符串是否为空(即长度是否为 0)[^1]。 - `isBlank` 判断字符串是否为空或仅包含空白字符[^3]。 - **处理空白字符**: - `isEmpty` 会将空白字符视为“空”。 - `isBlank` 会将空白字符视为“空”。 - **适用场景**: - 如果需要严格判断字符串是否为空(考虑空白字符),使用 `isEmpty`[^1]。 - 如果需要忽略空白字符并判断字符串是否真正有内容,使用 `isBlank`[^3]。 #### 4. **StringUtils 工具类对比** 除了标准库中的 `isEmpty` `isBlank`,Apache Commons Lang 提供了 `StringUtils` 工具类,其中也包含类似的方法。以下是其行为对比: - `StringUtils.isEmpty(String str)`:判断字符串是否为 `null` 或长度为 0[^3]。 - `StringUtils.isBlank(CharSequence cs)`:判断字符串是否为 `null`、长度为 0 或仅包含空白字符[^4]。 ```java import org.apache.commons.lang3.StringUtils; System.out.println(StringUtils.isEmpty("")); // true System.out.println(StringUtils.isEmpty(" ")); // false System.out.println(StringUtils.isBlank(" ")); // true System.out.println(StringUtils.isBlank("abc")); // false ``` #### 5. **总结** - `isEmpty` 适用于简单的空字符串判断。 - `isBlank` 更适合复杂的场景,例如需要忽略空白字符的情况。 - 如果项目中使用了 Apache Commons Lang,可以考虑使用 `StringUtils` 提供的工具方法以增强兼容性。 ```java public class Main { public static void main(String[] args) { System.out.println("".isEmpty()); // true System.out.println(" ".isEmpty()); // false System.out.println("".isBlank()); // true System.out.println(" ".isBlank()); // true } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值