1. 程式人生 > 其它 >學習記錄2

學習記錄2

StringUtils位置:org.apache.commons.lang3.StringUtils

isEmpty和isBlank還是有些區別的:

isEmpty原始碼:

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

isBlank原始碼:

/**
 * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
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;
}
空串:即不包含任何元素的字串,表示為"" 空格:即" ",對應ascii碼是32 空白字元:是一組非可見的字元,對於文字處理來說,除了空格外,通常包括tab(\t)、回車(\r)、換行(\n)、垂直製表符(\v)、換頁符(\f),windows系統常用的回車換行的組合(\r\n)也算在其中
總結就是:isBlank是嚴格意義上的判空操作,而isEmpty無法判斷出來空格,例如:"  "

  

更加詳細的說明可以參照:https://www.cnblogs.com/sealy321/p/10227131.html

官方API文件:https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html