美文网首页
StringUtils

StringUtils

作者: 6默默Welsh | 来源:发表于2019-06-05 17:06 被阅读0次

StringUtils.isBlank()检验String 类型的变量是否为空

在校验一个String类型的变量是否为空时,通常存在3中情况:

  1. 是否为 null
  2. 是否为 ""
  3. 是否为空字符串(引号中间有空格) 如: " "

StringUtils的isBlank()方法可以一次性校验这三种情况,返回值都是true

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

当受检查的值时 null 时,返回true,当受检查值时 ""时,返回值时true,当受检查值是空字符串时,返回值是true

StringUtils.isEmpty(String str) 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
StringUtils.isBlank(String str) 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成

相关文章

网友评论

      本文标题:StringUtils

      本文链接:https://www.haomeiwen.com/subject/fzmhwqtx.html