StringUtils.isEmpty()方法与StringUtils.isBlank()方法的区别与理解

一、从源码上看

 

我们先从源码定义上来看区别:

// Empty checks
    //-----------------------------------------------------------------------
    /**
     * <p>Checks if a String is empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     *
     * <p>NOTE: This method changed in Lang version 2.0.
     * It no longer trims the String.
     * That functionality is available in isBlank().</p>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is empty or null
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

从源码上可以看到,isEmpty()方法检测:①字符串是不是空字符串(""),②字符串是不是不存在(null)。并列举了一些例子

/**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

从isBlank()方法的源码上可以看出,它比isEmpty()方法多检测一个情况,在isEmpty()方法业务的基础上,再加上的了一个判断:③判断字符串是不是空格符(" ")(从代码上可以看出,多个空格也返回true)

 

注意:上面提到的判断,都是或的关系,满足其一都返回true,否则返回false

 

二、从实例上看

 

这里列举几个例子,看看是否与预期相同

String test1 = "";
String test2 = " ";
String test3 = new String("");
String test4 = new String(" ");
String test5 = null;

/**
* blank检测空字符串、null和空格     empty检测空字符串和null
* empty说:什么是空?空字符串、null是空
* blank说:什么是空白?空字符串、null、空格是空白
*/
System.out.println("test1 is empty: " + StringUtils.isEmpty(test1));
System.out.println("test1 is blank: " + StringUtils.isBlank(test1));
		
System.out.println("test2 is empty: " + StringUtils.isEmpty(test2));
System.out.println("test2 is blank: " + StringUtils.isBlank(test2));
		
System.out.println("test3 is empty: " + StringUtils.isEmpty(test3));
System.out.println("test3 is blank: " + StringUtils.isBlank(test3));
		
System.out.println("test4 is empty: " + StringUtils.isEmpty(test4));
System.out.println("test4 is blank: " + StringUtils.isBlank(test4));
		
System.out.println("test5 is empty: " + StringUtils.isEmpty(test5));
System.out.println("test5 is blank: " + StringUtils.isBlank(test5));



执行结果:
test1 is empty: true
test1 is blank: true

test2 is empty: false
test2 is blank: true

test3 is empty: true
test3 is blank: true

test4 is empty: false
test4 is blank: true

test5 is empty: true
test5 is blank: true

从执行结果上我们可以看到与我们的预期是没有区别的,与指向的内存区域是没有关系的(也就是怎么创建的)

 

三、从其他方面看

 

empty的英文意思是空的

blank的英文意思是空白的

空格符也是一个字符,所以他不是空的,但是他是空白的,因为他的打印结果是看不到的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值