StringUtils類API及使用方法詳解

StringUtils類API及使用方法詳解

StringUtils方法概覽

判空函數

1)StringUtils.isEmpty(String str)css

2)StringUtils.isNotEmpty(String str)java

3)StringUtils.isBlank(String str)api

4)StringUtils.isNotBlank(String str)數組

5)StringUtils.isAnyBlank(CharSequence… css)app

6)StringUtils.isAnyEmpty(CharSequence… css)dom

7)StringUtils.isNoneBlank(CharSequence… css)函數

8)StringUtils.isNoneEmpty(CharSequence… css)ui

9)StringUtils.isWhitespace(CharSequence cs)spa

  • isNotEmpty = !isEmpty, isBlank同理;
  • 容易忽略的;
    StringUtils.isEmpty("") = true
  • isBlank和isEmpty區別:
    1.  
      System .out.println(StringUtils.isBlank(" "));//true
    2.  
       
    3.  
      System .out.println(StringUtils.isBlank(" "));//true
    4.  
       
    5.  
      System .out.println(StringUtils.isBlank("\n\t"));//true
    6.  
      //區別
    7.  
      StringUtils .isEmpty(" ") = false
  • isAnyBlank和isAnyEmpty是多維判空,存在一個blank或者empty既true
    1.  
      StringUtils .isAnyBlank("", "bar","foo"); = true
    2.  
      //注意這兩個區別
    3.  
      StringUtils .isAnyEmpty(" ", "bar") = false
    4.  
      StringUtils .isAnyEmpty(" ", "bar") = true
  • isNoneBlank = !isAnyBlank;isNoneEmpty同理
    1.  
      public static boolean isNoneBlank(CharSequence... css) {
    2.  
      return !isAnyBlank(css);
    3.  
      }
  • isWhitespace判斷空白
    1.  
      StringUtils .isWhitespace(null) = false
    2.  
      StringUtils .isWhitespace("") = true
    3.  
      StringUtils .isWhitespace(" ") = true

大小寫函數

StringUtils.capitalize(String str)code

StringUtils.uncapitalize(String str)

StringUtils.upperCase(String str)

StringUtils.upperCase(String str,Locale locale)

StringUtils.lowerCase(String str)

StringUtils.lowerCase(String str,Locale locale)

StringUtils.swapCase(String str)

StringUtils.isAllUpperCase(CharSequence cs)

StringUtils.isAllLowerCase(CharSequence cs)

注意點:

  • capitalize首字母大寫,upperCase所有轉化爲大寫,swapCase大小寫互轉;
    1.  
      StringUtils .capitalize(null) = null
    2.  
       
    3.  
      StringUtils .capitalize("") = ""
    4.  
       
    5.  
      //首字母轉爲大寫
    6.  
      StringUtils .capitalize("cat") = "Cat"
    7.  
       
    8.  
      //所有轉爲大寫
    9.  
      StringUtils .upperCase("aBc") = "ABC"
    10.  
       
    11.  
      //大小寫互轉
    12.  
      StringUtils .swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
  • isAllUpperCase是否所有大寫,isAllLowerCase是否所有小寫
    1.  
      StringUtils .isAllLowerCase(" ") = false
    2.  
       
    3.  
      StringUtils .isAllLowerCase("abc") = true
    4.  
       
    5.  
      StringUtils .isAllLowerCase("abC") = false
    6.  
       
    7.  
      StringUtils .isAllLowerCase("ab c") = false
    8.  
       
    9.  
      StringUtils .isAllLowerCase("ab1c") = false
    10.  
       
    11.  
      StringUtils .isAllLowerCase("ab/c") = false

刪除函數

StringUtils.remove(String str, char remove)

StringUtils.remove(String str, String remove)

StringUtils.removeEnd(String str, String remove)

StringUtils.removeEndIgnoreCase(String str, String remove)

StringUtils.removePattern(String source, String regex)

StringUtils.removeStart(String str, String remove)

StringUtils.removeStartIgnoreCase(String str, String remove)

StringUtils.deleteWhitespace(String str)

  • 具體例子
    1.  
      //刪除字符
    2.  
      StringUtils .remove("queued", 'u') = "qeed"
    3.  
       
    4.  
      //刪除字符串
    5.  
      StringUtils .remove("queued", "ue") = "qd"
    6.  
       
    7.  
      //刪除結尾匹配的字符串
    8.  
      StringUtils .removeEnd("www.domain.com", ".com") = "www.domain"
    9.  
       
    10.  
      //刪除結尾匹配的字符串,找都不到返回原字符串
    11.  
      StringUtils .removeEnd("www.domain.com", "domain") = "www.domain.com"
    12.  
       
    13.  
      //忽略大小寫的
    14.  
      StringUtils .removeEndIgnoreCase("www.domain.com", ".COM") = "www.domain")
    15.  
       
    16.  
      //刪除全部空白(好用)
    17.  
      StringUtils .deleteWhitespace("abc") = "abc"
    18.  
      StringUtils .deleteWhitespace(" ab c ") = "abc"

替換函數

replace(String text, String searchString, String replacement)

replace(String text, String searchString, String replacement, int max)

replaceChars(String str, char searchChar, char replaceChar)

replaceChars(String str, String searchChars, String replaceChars)

replaceEach(String text, String[] searchList, String[] replacementList)

replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)

replaceOnce(String text, String searchString, String replacement)

replacePattern(String source, String regex, String replacement)

overlay(String str,String overlay,int start,int end)

  • 例子
    1.  
      StringUtils .replace("aba", "a", "") = "b"
    2.  
      StringUtils .replace("aba", "a", "z") = "zbz"
    3.  
       
    4.  
      //數字就是替換個數,0表明不替換,1表明從開始數起第一個,-1表明所有替換
    5.  
      StringUtils .replace("abaa", "a", "", -1) = "b"
    6.  
      StringUtils .replace("abaa", "a", "z", 0) = "abaa"
    7.  
      StringUtils .replace("abaa", "a", "z", 1) = "zbaa"
    8.  
      StringUtils .replace("abaa", "a", "z", 2) = "zbza"
    9.  
      StringUtils .replace("abaa", "a", "z", -1) = "zbzz"
  • replaceEach是replace的加強版,搜索列表和替換長度必須一致,不然報IllegalArgumentException異常:
    1.  
      StringUtils .replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
    2.  
      StringUtils .replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "dcte"
    3.  
      StringUtils .replaceChars("dabcba", "bcd", "yzx") = "xayzya"
    4.  
      StringUtils .replaceChars("abcba", "bc", "y") = "ayya"

史上最詳盡的Land3 StringUtils類使用方法大全1

replaceChars原理圖
  • replaceOnce只替換一次:
    1.  
      StringUtils .replaceOnce("aba", "a", "") = "ba"
    2.  
      StringUtils .replaceOnce("aba", "a", "z") = "zba"
    3.  
      StringUtils .replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
    4.  
      //這是一個很是奇怪的函數,原本本身料想結果應該是「dcte」的,但是結果竟然是tcte,而dcte再替換一次就是tcte了。
    5.  
      StringUtils .replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "tcte"
  1.  
    StringUtils .overlay("abcdef", "zzzz", 2, 4) = "abzzzzef"
  2.  
    StringUtils .overlay("abcdef", "zzzz", 4, 2) = "abzzzzef"
  3.  
    StringUtils .overlay("abcdef", "zzzz", -1, 4) = "zzzzef"
  4.  
    StringUtils .overlay("abcdef", "zzzz", 2, 8) = "abzzzz"
  5.  
    StringUtils .overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef"
  6.  
    StringUtils .overlay("abcdef", "zzzz", 8, 10) = "abcdefzzzz"

反轉函數

reverse(String str)

reverseDelimited(String str, char separatorChar)

例:

  1.  
    StringUtils .reverse("bat") = "tab"
  2.  
    StringUtils .reverseDelimited("a.b.c", 'x') = "a.b.c"
  3.  
    StringUtils .reverseDelimited("a.b.c", ".") = "c.b.a"

分隔合併函數

split(String str)

split(String str, char separatorChar)

split(String str, String separatorChars)

split(String str, String separatorChars, int max)

splitByCharacterType(String str)

splitByCharacterTypeCamelCase(String str)

splitByWholeSeparator(String str, String separator)

splitByWholeSeparator(String str, String separator, int max)

splitByWholeSeparatorPreserveAllTokens(String str, String separator)

splitByWholeSeparatorPreserveAllTokens(String str, String separator, int max)

splitPreserveAllTokens(String str)

splitPreserveAllTokens(String str, char separatorChar)

splitPreserveAllTokens(String str, String separatorChars)

splitPreserveAllTokens(String str, String separatorChars, int max)

例:

  1.  
    //用空白符作空格
  2.  
    StringUtils .split("abc def") = ["abc", "def"]
  3.  
     
  4.  
    StringUtils .split("abc def") = ["abc", "def"]
  5.  
     
  6.  
    StringUtils .split("a..b.c", '.') = ["a", "b", "c"]
  7.  
    //用字符分割
  8.  
    StringUtils .split("a:b:c", '.') = ["a:b:c"]
  9.  
    //0 或者負數表明沒有限制
  10.  
    StringUtils .split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"]
  11.  
    //分割字符串 ,能夠設定獲得數組的長度,限定爲2
  12.  
    StringUtils .split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
  13.  
    //null也能夠做爲分隔
  14.  
    StringUtils .splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"]
  15.  
    StringUtils .splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"]
  16.  
    StringUtils .splitByWholeSeparator("ab:cd:ef", ":") = ["ab", "cd", "ef"]
  17.  
    StringUtils .splitByWholeSeparator("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
  18.  
    //帶有限定長度的分隔
  19.  
    StringUtils .splitByWholeSeparator("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]

join(byte[] array,char separator)

join(Object[] array,char separator)等方法

join(Object[] array,char separator,int startIndex,int endIndex)

例:

  1.  
    //只有一個參數的join,簡單合併在一塊兒
  2.  
    StringUtils .join(["a", "b", "c"]) = "abc"
  3.  
    StringUtils .join([null, "", "a"]) = "a"
  4.  
    //null的話,就是把字符合並在一塊兒
  5.  
    StringUtils .join(["a", "b", "c"], null) = "abc"
  6.  
    //從index爲0到3合併,注意是排除3的
  7.  
    StringUtils .join([null, "", "a"], ',', 0, 3) = ",,a"
  8.  
    StringUtils .join(["a", "b", "c"], "--", 0, 3) = "a--b--c"
  9.  
    //從index爲1到3合併,注意是排除3的
  10.  
    StringUtils .join(["a", "b", "c"], "--", 1, 3) = "b--c"
  11.  
    StringUtils .join(["a", "b", "c"], "--", 2, 3) = "c"

截取函數

substring(String str,int start)

substringAfter(String str,String separator)

substringBeforeLast(String str,String separator)

substringAfterLast(String str,String separator)

substringBetween(String str,String tag)

  1.  
    StringUtils .substring("abcdefg", 0) = "abcdefg"
  2.  
    StringUtils .substring("abcdefg", 2) = "cdefg"
  3.  
    StringUtils .substring("abcdefg", 4) = "efg"
  4.  
    //start>0表示從左向右, start<0表示從右向左, start=0則從左第一位開始
  5.  
    StringUtils .substring("abcdefg", -2) = "fg"
  6.  
    StringUtils .substring("abcdefg", -4) = "defg"
  7.  
     
  8.  
    //從第二個參數字符串開始截取,排除第二個字符串
  9.  
    StringUtils .substringAfter("abc", "a") = "bc"
  10.  
    StringUtils .substringAfter("abcba", "b") = "cba"
  11.  
    StringUtils .substringAfter("abc", "c") = ""
  12.  
     
  13.  
    //從最後一個字母出現開始截取
  14.  
    StringUtils .substringBeforeLast("abcba", "b") = "abc"
  15.  
    StringUtils .substringBeforeLast("abc", "c") = "ab"
  16.  
    StringUtils .substringBeforeLast("a", "a") = ""
  17.  
    StringUtils .substringBeforeLast("a", "z") = "a"
  18.  
     
  19.  
     
  20.  
    StringUtils .substringAfterLast("abc", "a") = "bc"
  21.  
    StringUtils .substringAfterLast("abcba", "b") = "a"
  22.  
    StringUtils .substringAfterLast("abc", "c") = ""
  23.  
     
  24.  
    StringUtils .substringBetween("tagabctag", null) = null
  25.  
    StringUtils .substringBetween("tagabctag", "") = ""
  26.  
    StringUtils .substringBetween("tagabctag", "tag") = "abc"

截取分析圖:

史上最詳盡的Land3 StringUtils類使用方法大全2

截取分析圖
  1.  
    // start>0&&end>0從左開始(包括左)到右結束(不包括右),
  2.  
    //start<0&&end<0從右開始(包括右),再向左數到end結束(包括end)
  3.  
    StringUtils .substring("abc", -2, -1) = "b"
  4.  
    //這個我至今還沒弄明白
  5.  
    StringUtils .substring("abc", -4, 2) = "ab"

類似度函數

一個字符串能夠經過增長一個字符,刪除一個字符,替換一個字符獲得另一個字符串,假設,咱們把從字符串A轉換成字符串B,前面3種操做所執行的最少次數稱爲AB類似度.

getLevenshteinDistance(CharSequence s, CharSequence t)
getLevenshteinDistance(CharSequence s, CharSequence t, int threshold)
StringUtils.getLevenshteinDistance(「elephant」, 「hippo」) = 7
StringUtils.getLevenshteinDistance(「hippo」, 「elephant」) = 7

例:
//b替換爲d
abc adc 度爲 1
//ababababa去掉a,末尾加b
ababababa babababab 度爲 2
abcd acdb 度爲2

差別函數

difference(String str1,String str2)

  1.  
    //在str1中尋找str2中沒有的的字符串,並返回
  2.  
    StringUtils .difference("", "abc") = "abc"
  3.  
    StringUtils .difference("abc", "") = ""
  4.  
    StringUtils .difference("abc", "abc") = ""
  5.  
    StringUtils .difference("abc", "ab") = ""
  6.  
    StringUtils .difference("ab", "abxyz") = "xyz"
  7.  
    StringUtils .difference("abcde", "abxyz") = "xyz"
  8.  
    StringUtils .difference("abcde", "xyz") = "xyz"

圖片理解:

史上最詳盡的Land3 StringUtils類使用方法大全3

差別圖片

縮短省略函數

abbreviate(String str, int maxWidth)

abbreviate(String str, int offset, int maxWidth)

abbreviateMiddle(String str, String middle, int length)

注意:

  • 字符串的長度小於或等於最大長度,返回該字符串。
  • 運算規律(substring(str, 0, max-3) + 「…」)
  • 若是最大長度小於4,則拋出異常。
    1.  
      //(substring(str, 0, 6-3) + "...")
    2.  
      StringUtils .abbreviate("abcdefg", 6) = "abc..."
    3.  
      StringUtils .abbreviate("abcdefg", 7) = "abcdefg"
    4.  
      StringUtils .abbreviate("abcdefg", 8) = "abcdefg"
    5.  
      StringUtils .abbreviate("abcdefg", 4) = "a..."
    6.  
      StringUtils .abbreviate("abcdefg", 3) = IllegalArgumentException

匹配計數函數

countMatches(CharSequence str,char ch)

  1.  
    StringUtils .countMatches("abba", 0) = 0
  2.  
    StringUtils .countMatches("abba", 'a') = 2
  3.  
    StringUtils .countMatches("abba", 'b') = 2
  4.  
    StringUtils .countMatches("abba", 'x') = 0

刪除空白函數

trim(String str)

trimToEmpty(String str)

trimToNull(String str)

deleteWhitespace(String str)

  1.  
    StringUtils .trim(" ") = ""
  2.  
    StringUtils .trim("abc") = "abc"
  3.  
    StringUtils .trim(" abc ") = "abc"
  4.  
    //空的話,返回null
  5.  
    StringUtils .trimToNull(" ") = null
  6.  
    StringUtils .trimToNull("abc") = "abc"
  7.  
    StringUtils .trimToNull(" abc ") = "abc"
  8.  
    StringUtils .trimToEmpty(" ") = ""
  9.  
    StringUtils .trimToEmpty("abc") = "abc"
  10.  
    StringUtils .trimToEmpty(" abc ") = "abc"

-注意這二者的區別。

  1.  
    StringUtils .deleteWhitespace("") = ""
  2.  
    StringUtils .deleteWhitespace("abc") = "abc"
  3.  
    StringUtils .deleteWhitespace(" ab c ") = "abc"

判斷是否相等函數

equals(CharSequence cs1,CharSequence cs2)

equalsIgnoreCase(CharSequence str1, CharSequence str2)

  1.  
    StringUtils .equals("abc", null) = false
  2.  
    StringUtils .equals("abc", "abc") = true
  3.  
    StringUtils .equals("abc", "ABC") = false
  4.  
    //忽略大小寫
  5.  
    StringUtils .equalsIgnoreCase("abc", null) = false
  6.  
    StringUtils .equalsIgnoreCase("abc", "abc") = true
  7.  
    StringUtils .equalsIgnoreCase("abc", "ABC") = true

默認字符函數

defaultString(String str)

defaultString(String str,String defaultStr)

  1.  
    StringUtils .defaultString("") = ""
  2.  
    StringUtils .defaultString("bat") = "bat"
  3.  
    StringUtils .defaultString("", "NULL") = ""
  4.  
    //若是第一個參數爲空,這返回第二個默認參數
  5.  
    StringUtils .defaultString("bat", "NULL") = "bat"

填充居中函數

leftPad/rightPad(String str,int size)

leftPad(String str,int size,char padChar)

center(String str,int size)

center(String str,int size,char padChar)

repeat(char ch,int repeat)

repeat(String str,String separator,int repeat)

appendIfMissing(String str, CharSequence suffix, CharSequence… suffixes)

appendIfMissing(String str,CharSequence suffix,CharSequence… suffixes)

  1.  
    StringUtils .leftPad("bat", 3) = "bat"
  2.  
    //左填充,默認填充空
  3.  
    StringUtils .leftPad("bat", 5) = " bat"
  4.  
    StringUtils .leftPad("bat", 1) = "bat"
  5.  
    StringUtils .leftPad("bat", -1) = "bat"
  6.  
    //左填充,填充字符爲z
  7.  
    StringUtils .leftPad("bat", 3, 'z') = "bat"
  8.  
    StringUtils .leftPad("bat", 5, 'z') = "zzbat"
  9.  
    StringUtils .leftPad("bat", 1, 'z') = "bat"
  10.  
    StringUtils .leftPad("bat", -1, 'z') = "bat"
  11.  
    //居中
  12.  
    StringUtils .center("ab", -1) = "ab"
  13.  
    StringUtils .center("ab", 4) = " ab "
  14.  
    StringUtils .center("abcd", 2) = "abcd"
  15.  
    StringUtils .center("a", 4) = " a
  16.  
    //居中,最後一個參數是填充字符或字符串
  17.  
    StringUtils .center("abcd", 2, ' ') = "abcd"
  18.  
    StringUtils .center("a", 4, ' ') = " a "
  19.  
    StringUtils .center("a", 4, 'y') = "yayy"
  20.  
    //重複字符串,第二個參數是重複次數
  21.  
    StringUtils .repeat("a", 3) = "aaa"
  22.  
    StringUtils .repeat("ab", 2) = "abab"
  23.  
    StringUtils .repeat("a", -2) = ""
  24.  
    //重複字符串,第二個參數是分割符,第三個參數是重複次數
  25.  
    StringUtils .repeat("", "x", 3) = "xxx"
  26.  
    StringUtils .repeat("?", ", ", 3) = "?, ?, ?"
  27.  
     
  28.  
    //
  29.  
    StringUtils .appendIfMissing("abcxyz", "xyz", "mno") = "abcxyz"
  30.  
    StringUtils .appendIfMissing("abcmno", "xyz", "mno") = "abcmno"
  31.  
    StringUtils .appendIfMissing("abcXYZ", "xyz", "mno") = "abcXYZxyz"
  32.  
    StringUtils .appendIfMissing("abcMNO", "xyz", "mno") = "abcMNOxyz"

是否包含函數

containsOnly(CharSequence cs,char… valid)

containsNone(CharSequence cs,char… searchChars)

startsWith(CharSequence str,CharSequence prefix)

startsWithIgnoreCase(CharSequence str,CharSequence prefix)

startsWithAny(CharSequence string,CharSequence… searchStrings)

  • 例子
    1.  
      //判斷字符串中全部字符,是否都是出自參數2中
    2.  
      StringUtils .containsOnly("ab", "") = false
    3.  
      StringUtils .containsOnly("abab", "abc") = true
    4.  
      StringUtils .containsOnly("ab1", "abc") = false
    5.  
      StringUtils .containsOnly("abz", "abc") = false
    6.  
       
    7.  
      //判斷字符串中全部字符,都不在參數2中。
    8.  
      StringUtils .containsNone("abab", 'xyz') = true
    9.  
      StringUtils .containsNone("ab1", 'xyz') = true
    10.  
      StringUtils .containsNone("abz", 'xyz') = false
    11.  
       
    12.  
      //判斷字符串是否以第二個參數開始
    13.  
      StringUtils .startsWith("abcdef", "abc") = true
    14.  
      StringUtils .startsWith("ABCDEF", "abc") = false

索引下標函數

indexOf(CharSequence seq,CharSequence searchSeq)

indexOf(CharSequence seq,CharSequence searchSeq,int startPos)

indexOfIgnoreCase/lastIndexOfIgnoreCase(CharSequence str,CharSequence searchStr)

lastIndexOf(CharSequence seq,int searchChar)

    1.  
      //返回第二個參數開始出現的索引值
    2.  
      StringUtils .indexOf("aabaabaa", "a") = 0
    3.  
      StringUtils .indexOf("aabaabaa", "b") = 2
    4.  
      StringUtils .indexOf("aabaabaa", "ab") = 1
    5.  
       
    6.  
      //從第三個參數索引開始找起,返回第二個參數開始出現的索引值
    7.  
      StringUtils .indexOf("aabaabaa", "a", 0) = 0
    8.  
      StringUtils .indexOf("aabaabaa", "b", 0) = 2
    9.  
      StringUtils .indexOf("aabaabaa", "ab", 0) = 1
    10.  
      StringUtils .indexOf("aabaabaa", "b", 3) = 5
    11.  
      StringUtils .indexOf("aabaabaa", "b", 9) = -1
    12.  
      //返回第二個參數出現的最後一個索引值
    13.  
      StringUtils .lastIndexOf("aabaabaa", 'a') = 7
    14.  
      StringUtils .lastIndexOf("aabaabaa", 'b') = 5
    15.  
       
    16.  
      StringUtils .lastIndexOfIgnoreCase("aabaabaa", "A", 8) = 7
    17.  
      StringUtils .lastIndexOfIgnoreCase("aabaabaa", "B", 8) = 5
    18.  
      StringUtils .lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
    19.  
      StringUtils .lastIndexOfIgnoreCase("aabaabaa", "B", 9) = 5
相關文章
相關標籤/搜索