码迷,mamicode.com
首页 > 其他好文 > 详细

StringUtils 中 isEmpty 和 isBlank 的区别

时间:2017-02-25 01:08:20      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:i++   har   工作   ring   div   str   大致   区别   字符   

  在项目的工作学习中经常用到了 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 为假。

StringUtils 中 isEmpty 和 isBlank 的区别

标签:i++   har   工作   ring   div   str   大致   区别   字符   

原文地址:http://www.cnblogs.com/brother-four/p/6438591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!