StringUtils isEmpty 和 isBlank 的区别 CollectionUtils判空的方法

本文讨论的 StringUtils 属于package org.apache.commons.lang;

字符串判空检查

要了解字符串判空方法的区别首先要理解对象为空字符串"" 和 null 的区别

“” 和 null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。
  • “” 空字符串是一个 String 对象是有地址的,只是内容是空。

关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0 , 布尔型是 false,对象引用则是 null。
String 并不是基本数据类型,而是对象所以会被默认的赋予 null。

isEmpty() 和 isBlank() 区别在于 isBlank() 可以多了对于空格的判断,可以根据方法名区别使用 isEmpty() 判断字符串是否为空,而 isBlank() 则是判断字符串是否是空格,空或null

isEmpty(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回false

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

isBlank(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回true

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

本文讨论的 StringUtils 属于package org.apache.commons.lang;

字符串判空检查

要了解字符串判空方法的区别首先要理解对象为空字符串"" 和 null 的区别

“” 和 null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。
  • “” 空字符串是一个 String 对象是有地址的,只是内容是空。

关于构造器初始化,在没有显示赋予初值的情况下。默认将数值型赋为 0 , 布尔型是 false,对象引用则是 null。
String 并不是基本数据类型,而是对象所以会被默认的赋予 null。

isEmpty() 和 isBlank() 区别在于 isBlank() 可以多了对于空格的判断,可以根据方法名区别使用 isEmpty() 判断字符串是否为空,而 isBlank() 则是判断字符串是否是空格,空或null

isEmpty(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回false

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

isBlank(String str)

isEmpty(), isNotEmpty() 判段字符串是否为空或者null,空格返回true

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

本文讨论的 CollectionUtils 属于package org.apache.commons.collections;

集合判空检查

要了解集合判空方法的区别首先要理解对象为size == 0 和 null 的区别

size==0 和 null 的区别

  • null 是没有地址的,可以理解为空指针。当对象在构造器初始化时,如果没有被显示的赋于初值,那么会默认赋值为 null。
  • size==0 表示集合已经指向一个地址,但是指向的对象中没有元素。

isEmpty() 和 isBlank() 区别在于 isBlank()

isEmpty(Collection coll)

isEmpty(), isNotEmpty() 判段集合是否为null或者不包含任何元素,null 返回true

/**
 * Null-safe check if the specified collection is empty.
 * <p>
 * Null returns true.
 * 
 * @param coll  the collection to check, may be null
 * @return true if empty or null
 * @since Commons Collections 3.2
 */
public static boolean isEmpty(Collection coll) {
    return (coll == null || coll.isEmpty());
}

/**
 * Returns <tt>true</tt> if this collection contains no elements.
 *
 * @return <tt>true</tt> if this collection contains no elements
 */
boolean isEmpty();

猜你喜欢

转载自blog.csdn.net/mochi_li/article/details/84992273