1. 程式人生 > 實用技巧 >org.apache.commons.lang3.StringUtils 原始碼解讀彙總

org.apache.commons.lang3.StringUtils 原始碼解讀彙總

一、背景

  不管是公司的專案,還是個人的日常練習,都會使用org.apache.commons.lang3.StringUtils 這個String工具類,究其原因,是太好用了,而且太全了。比如判斷是否為空,擷取,拼接之類的。一直沒有仔細的看過它的原始碼,今天我就研究一番,同時作為我“閱讀原始碼”路上的第一步。

//todo

什麼是CharSequence

//todo

‘’,null,‘ ’區別

二、常用API

  注:本次是基於commons-lang3-3.8.1 進行閱讀分析總結的,其他版本與此相差不多。

  

2.1、Empty

2.1.1、isEmpty:檢查字串是 "" 或 null

//原始碼
public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
}
@Test
public void isEmpty_Test() {
   //true
   StringUtils.isEmpty(null);
   //true
   StringUtils.isEmpty("");
   //false
   StringUtils.isEmpty("  ");
   //false
   StringUtils.isEmpty("bob");
   
//false StringUtils.isEmpty(" bob "); }

2.1.2、isNotEmpty:與isEmpty正好相反,判斷不為""且不為null   

//原始碼
public static boolean isNotEmpty(final CharSequence cs) {
     return !isEmpty(cs);
}  
@Test
public void isNotEmpty_Test() {
    //false
    StringUtils.isNotEmpty(null);
    //false
    StringUtils.isNotEmpty("");
    
//true StringUtils.isNotEmpty(" "); //true StringUtils.isNotEmpty("bob"); //true StringUtils.isNotEmpty(" bob "); }

2.1.3、isAnyEmpty

2.1.4、isNoneEmpty

2.1.5、isAllEmpty

2.2、Blank

2.2.1、isBlank:檢查一個字串是 "",或null,或只有空格

與isEmpty相比,對於“ ”,isEmpty(" ") =false,isBlank(" ") =true, 

//原始碼
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))) {
            return false;
        }
    }
    return true;
} 
@Test
public void isBlank_Test() {
    //true
    StringUtils.isBlank(null);
    //true
    StringUtils.isBlank("");
    //true
    StringUtils.isBlank(" ");
    //false
    StringUtils.isBlank("bob");
    //false
    StringUtils.isBlank("  bob  ");
}

2.2.2、isNotBlank:檢查一個字串不是 "",且不是null,且不只有空格   

@Test
public void isNotBlank_Test() {
    //false
    StringUtils.isNotBlank(null);
    //false
    StringUtils.isNotBlank("");
    //false
    StringUtils.isNotBlank(" ");
    //true
    StringUtils.isNotBlank("bob");
    //true
    StringUtils.isNotBlank("  bob  ");
}

2.2.3、isAnyBlank

2.2.4、isNoneBlank

2.2.5、isAllBlank

2.3、Trim

2.3.1、trim:移除字串兩端的空字串 

//原始碼
public static String trim(final String str) {
     return str == null ? null : str.trim();
} 
@Test
public void trim_Test() {
    //null    
    StringUtils.trim(null);
    //""             
    StringUtils.trim("");
    //""             
    StringUtils.trim("     ");
    //"abc"           
    StringUtils.trim("abc");
    //"abc"             
    StringUtils.trim("    abc    ");
}

2.3.2、trimToNull:trim之後,用isEmpty進行了判斷,如果true返回null,否則返回trim後的值  

//原始碼
public static String trimToNull(final String str) {
    final String ts = trim(str);
    return isEmpty(ts) ? null : ts;
}
StringUtils.trimToNull(null)          = null
StringUtils.trimToNull("")            = null
StringUtils.trimToNull("     ")       = null
StringUtils.trimToNull("abc")         = "abc"
StringUtils.trimToNull("    abc    ") = "abc"

2.3.3、trimToEmpty:如果為null,返回“”,否則返回trim後的值

//原始碼
public static String trimToEmpty(final String str) {
    return str == null ? EMPTY : str.trim();
}
StringUtils.trimToEmpty(null)          = ""
StringUtils.trimToEmpty("")            = ""
StringUtils.trimToEmpty("     ")       = ""
StringUtils.trimToEmpty("abc")         = "abc"
StringUtils.trimToEmpty("    abc    ") = "abc"

2.4、Trim

2.4.1、truncate:字串擷取,從索引為0處擷取,擷取固定的長度

//原始碼
public static String truncate(final String str, final int maxWidth) {
    return truncate(str, 0, maxWidth);
}
StringUtils.truncate(null, 0)       = null
StringUtils.truncate(null, 2)       = null
StringUtils.truncate("", 4)         = ""
StringUtils.truncate("abcdefg", 4)  = "abcd"
StringUtils.truncate("abcdefg", 6)  = "abcdef"
StringUtils.truncate("abcdefg", 7)  = "abcdefg"
StringUtils.truncate("abcdefg", 8)  = "abcdefg"
StringUtils.truncate("abcdefg", -1) = throws an IllegalArgumentException

     

    可以看到,對於起始擷取索引,擷取長度為負數時,直接報異常;然後被擷取字串為null時,返回null;擷取索引大於字串長度時,返回“”。

//原始碼
public static String truncate(final String str, final int offset, final int maxWidth) {
    if (offset < 0) {
        throw new IllegalArgumentException("offset cannot be negative");
    }
    if (maxWidth < 0) {
        throw new IllegalArgumentException("maxWith cannot be negative");
    }
    if (str == null) {
        return null;
    }
    if (offset > str.length()) {
        return EMPTY;
    }
    if (str.length() > maxWidth) {
        final int ix = offset + maxWidth > str.length() ? str.length() : offset + maxWidth;
        return str.substring(offset, ix);
    }
    return str.substring(offset);
}
StringUtils.truncate(null, 0, 0) = null
StringUtils.truncate(null, 2, 4) = null
StringUtils.truncate("", 0, 10) = ""
StringUtils.truncate("", 2, 10) = ""
StringUtils.truncate("abcdefghij", 0, 3) = "abc"
StringUtils.truncate("abcdefghij", 5, 6) = "fghij"
StringUtils.truncate("raspberry peach", 10, 15) = "peach"
StringUtils.truncate("abcdefghijklmno", 0, 10) = "abcdefghij"
StringUtils.truncate("abcdefghijklmno", -1, 10) = throws an IllegalArgumentException
StringUtils.truncate("abcdefghijklmno", Integer.MIN_VALUE, 10) = "abcdefghij"
StringUtils.truncate("abcdefghijklmno", Integer.MIN_VALUE, Integer.MAX_VALUE) = "abcdefghijklmno"
StringUtils.truncate("abcdefghijklmno", 0, Integer.MAX_VALUE) = "abcdefghijklmno"
StringUtils.truncate("abcdefghijklmno", 1, 10) = "bcdefghijk"
StringUtils.truncate("abcdefghijklmno", 2, 10) = "cdefghijkl"
StringUtils.truncate("abcdefghijklmno", 3, 10) = "defghijklm"
StringUtils.truncate("abcdefghijklmno", 4, 10) = "efghijklmn"
StringUtils.truncate("abcdefghijklmno", 5, 10) = "fghijklmno"
StringUtils.truncate("abcdefghijklmno", 5, 5) = "fghij"
StringUtils.truncate("abcdefghijklmno", 5, 3) = "fgh"
StringUtils.truncate("abcdefghijklmno", 10, 3) = "klm"
StringUtils.truncate("abcdefghijklmno", 10, Integer.MAX_VALUE) = "klmno"
StringUtils.truncate("abcdefghijklmno", 13, 1) = "n"
StringUtils.truncate("abcdefghijklmno", 13, Integer.MAX_VALUE) = "no"
StringUtils.truncate("abcdefghijklmno", 14, 1) = "o"
StringUtils.truncate("abcdefghijklmno", 14, Integer.MAX_VALUE) = "o"
StringUtils.truncate("abcdefghijklmno", 15, 1) = ""
StringUtils.truncate("abcdefghijklmno", 15, Integer.MAX_VALUE) = ""
StringUtils.truncate("abcdefghijklmno", Integer.MAX_VALUE, Integer.MAX_VALUE) = ""
StringUtils.truncate("abcdefghij", 3, -1) = throws an IllegalArgumentException
StringUtils.truncate("abcdefghij", -2, 4) = throws an IllegalArgumentException

2.5、Strip

2.5.1、strip

2.5.2、stripToNull

2.5.3、StripToEmpty

2.5.4、stripStart

2.5.5、stripEnd

2.5.6、stripAll

2.5.7、stripAccents

2.6、Equals、toString

2.6.1、equals

2.6.2、equalsIgnoreCase

2.6.3、equalsAny

2.6.4、equalsAnyIgnoreCase

2.6.5、toString

2.6.6、toEncodedString

2.7、Compare

2.7.1、compare

2.7.2、compareIgnoreCase

2.8、IndexOf

2.8.1、indexOf

2.8.2、ordinalIndexOf

2.8.3、indexOfIgnoreCase

2.8.4、lastIndexOf

2.8.5、lastOrdinalIndexOf

2.8.6、lastIndexOfIgnoreCase

2.8.7、indexOfAny

2.8.8、indexOfAnyBut

2.8.9、lastIndexOfAny

2.9、Contains

2.9.1、contains

2.9.2、containsIgnoreCase

2.9.3、containsWhitespace

2.9.4、containsAny

2.9.5、containsOnly

2.9.6、containsNone

2.10、Substring

2.10.1、substring

2.10.2、left

2.10.3、right

2.10.4、mid

2.10.5、substringBefore

2.10.6、substringAfter

2.10.7、substringBeforeLast

2.10.8、substringAfterLast

2.10.9、substringsBetween

2.11、Split

2.11.1、split

2.11.2、splitByWholeSeparator

2.11.3、splitByWholeSeparatorPreserveAllTokens

2.11.4、splitByWholeSeparatorWorker

2.11.5、splitPreserveAllTokens

2.11.6、splitWorker

2.11.7、splitByCharacterType

2.11.8、splitByCharacterTypeCamelCase

2.12、Join

2.12.1、join

2.12.1、joinWith

2.13、Delete

2.13.1、deleteWhitespace

2.14、Remove

2.14.1、remove

2.14.2、removeIgnoreCase

2.14.3、removeStart

2.14.4、removeStartIgnoreCase

2.14.5、removeEnd

2.14.6、removeEndIgnoreCase

2.14.7、removeAll

2.14.8、removeFirst

2.14.9、removePattern

2.15、Replace

2.15.1、replace

2.15.2、replaceIgnoreCase

2.15.3、replaceOnce

2.15.4、replaceOnceIgnoreCase

2.15.5、replacePattern

2.15.6、replaceAll

2.15.7、replaceFirst

2.15.8、replaceEach

2.15.9、replaceEachRepeatedly

2.15.10、replaceChars

2.16、Overlay

2.16.1、overlay

2.17、Chomping

2.17.1、chomp

2.18、Chop

2.18.1、chop

2.19、Repeat

2.19.1、repeat

2.20、Pad

2.20.1、rightPad

2.20.2、leftPad

2.21、Length

2.21.1、length

2.22、Center

2.22.1、center

2.23、Case

2.23.1、upperCase

2.23.2、lowerCase

2.23.3、swapCase

2.24、Capitalize

2.24.1、capitalize

2.24.2、uncapitalize

2.25、CountMatches

2.25.1、countMatches

2.26、判斷/檢查

2.26.1、isAlpha

2.26.2、isAlphaSpace

2.26.3、isAlphanumeric

2.26.4、isAlphanumericSpace

2.26.5、isAsciiPrintable

2.26.6、isNumeric

2.26.7、isNumericSpace

2.26.8、isWhitespace

2.26.9、isAllLowerCase

2.26.10、isAllUpperCase

2.26.11、isMixedCase



2.27、Default

2.27.1、defaultString

2.27.2、defaultIfBlank

2.27.3、defaultIfEmpty

2.28、Rotate

2.28.1、rotate

2.29、Reverse

2.29.1、reverse

2.29.1、reverseDelimited



2.30、Abbreviate

2.30.1、abbreviate

2.30.2、abbreviateMiddle



2.31、Difference

2.31.1、difference

2.31.2、indexOfDeffererce



2.32、開頭/結尾

2.32.1、startsWith

2.32.2、startsWithIgnoreCase

2.32.3、startsWithAny

2.32.4、endsWith

2.32.5、endsWithIgnoreCase

2.32.6、endsWithAny


2.33、追加

2.33.1、appendIfMissing

2.33.2、appendIfMissingIgnoreCase

2.33.3、prependIfMissing

2.33.4、prependIfMissingIgnoreCase

2.34、Wrap

2.34.1、wrap

2.34.2、wrapIfMissing

2.34.3、unwrap



2.35、其他

2.35.1、getCommonPrefix

2.35.2、getLevenshteinDistance

2.35.3、getJaroWinklerDistance

2.35.4、getFuzzyDistance

2.35.5、getDigits

2.35.6、firstNonBlank

2.35.7、firstNonEmpty

2.35.8、normalizeSpace

2.35.9、toCodePoints