• isEmpty 和 isBlank 的用法区别


    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>

    StringUtils.isEmpty()

    是否为空. 可以看到 " " 空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isEmpty(" ")=false

    StringUtils.isEmpty(null) = true
    StringUtils.isEmpty("") = true
    StringUtils.isEmpty(" ") = false
    StringUtils.isEmpty(“bob”) = false
    StringUtils.isEmpty(" bob ") = false
    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) {

    https://mp.weixin.qq.com/s/hlSQbTRRzLNZvr_eu_lT_g

    故乡明
  • 相关阅读:
    HTML4如何让一个DIV居中对齐?float输入日志标题
    HTML3层叠样式表
    面向对象 学生考试计分题目
    C#总复习
    HTML2列表表单框架
    HTML1网页三部份内容
    HTML 5 JavaScript初步 编译运行.doc
    初识MYSQL
    数据库设计
    序列化和反序列化
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/15132047.html
Copyright © 2020-2023  润新知