zoukankan      html  css  js  c++  java
  • StringUtils 中 isEmpty 和 isBlank 的区别

      在项目的工作学习中经常用到了 apache  commons 中的 StringUtils 的 isBlank 和 isEmpty 来判断字符串是否为空,这个方法都是判断字符串是否为空做判断的,以至于把我搞混了!!! 欲哭无泪啊,索性写个帖子记录下来。方便以后学习。

      不多说,我们直接看源码:

      isBlank:

    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;
    }

      isEmpty:

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

      接着我们看几个例子就一目了然了:

    //isBlank
    StringUtils.isBlank(null)      = true
    StringUtils.isBlank("")        = true
    StringUtils.isBlank(" ")       = true
    StringUtils.isBlank("bob")     = false
    StringUtils.isBlank("  bob  ") = false
    
    //isEmpty
    StringUtils.isEmpty(null)      = true
    StringUtils.isEmpty("")        = true
    StringUtils.isEmpty(" ")       = false
    StringUtils.isEmpty("bob")     = false
    StringUtils.isEmpty("  bob  ") = false

    我们可以看到大致没有什么太大的改动主要是对于空字符串的判断("  ").对于 isBlank为真而 isEmpty 为假。

  • 相关阅读:
    python生成随机密码
    python计算md5值
    python---连接MySQL第五页
    python---连接MySQL第四页
    梯度下降(Gradient Descent)小结
    矩阵的线性代数意义
    矩阵的意义
    偏导数与全导数的关系 以及 偏微分与全微分的关系
    mysql-blog
    python及numpy,pandas易混淆的点
  • 原文地址:https://www.cnblogs.com/brother-four/p/6438591.html
Copyright © 2011-2022 走看看