StringUtils類API及使用方法詳解
判空函數
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
- isBlank和isEmpty區別:
-
System
.out.println(StringUtils.isBlank(" "));
-
-
System
.out.println(StringUtils.isBlank(" "));
-
-
System
.out.println(StringUtils.isBlank("\n\t"));
-
-
StringUtils
.isEmpty(" ") = false
- isAnyBlank和isAnyEmpty是多維判空,存在一個blank或者empty既true
-
StringUtils
.isAnyBlank("", "bar","foo"); = true
-
-
StringUtils
.isAnyEmpty(" ", "bar") = false
-
StringUtils
.isAnyEmpty(" ", "bar") = true
- isNoneBlank = !isAnyBlank;isNoneEmpty同理
-
public static boolean isNoneBlank(CharSequence... css) {
-
-
- isWhitespace判斷空白
-
StringUtils
.isWhitespace(null) = false
-
StringUtils
.isWhitespace("") = true
-
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大小寫互轉;
-
StringUtils
.capitalize(null) = null
-
-
StringUtils
.capitalize("") = ""
-
-
-
StringUtils
.capitalize("cat") = "Cat"
-
-
-
StringUtils
.upperCase("aBc") = "ABC"
-
-
-
StringUtils
.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
- isAllUpperCase是否所有大寫,isAllLowerCase是否所有小寫
-
StringUtils
.isAllLowerCase(" ") = false
-
-
StringUtils
.isAllLowerCase("abc") = true
-
-
StringUtils
.isAllLowerCase("abC") = false
-
-
StringUtils
.isAllLowerCase("ab c") = false
-
-
StringUtils
.isAllLowerCase("ab1c") = false
-
-
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)
- 具體例子
-
-
StringUtils
.remove("queued", 'u') = "qeed"
-
-
-
StringUtils
.remove("queued", "ue") = "qd"
-
-
-
StringUtils
.removeEnd("www.domain.com", ".com") = "www.domain"
-
-
-
StringUtils
.removeEnd("www.domain.com", "domain") = "www.domain.com"
-
-
-
StringUtils
.removeEndIgnoreCase("www.domain.com", ".COM") = "www.domain")
-
-
-
StringUtils
.deleteWhitespace("abc") = "abc"
-
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)
- 例子
-
StringUtils
.replace("aba", "a", "") = "b"
-
StringUtils
.replace("aba", "a", "z") = "zbz"
-
-
-
StringUtils
.replace("abaa", "a", "", -1) = "b"
-
StringUtils
.replace("abaa", "a", "z", 0) = "abaa"
-
StringUtils
.replace("abaa", "a", "z", 1) = "zbaa"
-
StringUtils
.replace("abaa", "a", "z", 2) = "zbza"
-
StringUtils
.replace("abaa", "a", "z", -1) = "zbzz"
- replaceEach是replace的加強版,搜索列表和替換長度必須一致,不然報IllegalArgumentException異常:
-
StringUtils
.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
-
StringUtils
.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "dcte"
-
StringUtils
.replaceChars("dabcba", "bcd", "yzx") = "xayzya"
-
StringUtils
.replaceChars("abcba", "bc", "y") = "ayya"
replaceChars原理圖
- replaceOnce只替換一次:
-
StringUtils
.replaceOnce("aba", "a", "") = "ba"
-
StringUtils
.replaceOnce("aba", "a", "z") = "zba"
-
StringUtils
.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
-
-
StringUtils
.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "tcte"
-
StringUtils
.overlay("abcdef", "zzzz", 2, 4) = "abzzzzef"
-
StringUtils
.overlay("abcdef", "zzzz", 4, 2) = "abzzzzef"
-
StringUtils
.overlay("abcdef", "zzzz", -1, 4) = "zzzzef"
-
StringUtils
.overlay("abcdef", "zzzz", 2, 8) = "abzzzz"
-
StringUtils
.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef"
-
StringUtils
.overlay("abcdef", "zzzz", 8, 10) = "abcdefzzzz"
反轉函數
reverse(String str)
reverseDelimited(String str, char separatorChar)
例:
-
StringUtils
.reverse("bat") = "tab"
-
StringUtils
.reverseDelimited("a.b.c", 'x') = "a.b.c"
-
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)
例:
-
-
StringUtils
.split("abc def") = ["abc", "def"]
-
-
StringUtils
.split("abc def") = ["abc", "def"]
-
-
StringUtils
.split("a..b.c", '.') = ["a", "b", "c"]
-
-
StringUtils
.split("a:b:c", '.') = ["a:b:c"]
-
-
StringUtils
.split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"]
-
-
StringUtils
.split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
-
-
StringUtils
.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"]
-
StringUtils
.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"]
-
StringUtils
.splitByWholeSeparator("ab:cd:ef", ":") = ["ab", "cd", "ef"]
-
StringUtils
.splitByWholeSeparator("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
-
-
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)
例:
-
-
StringUtils
.join(["a", "b", "c"]) = "abc"
-
StringUtils
.join([null, "", "a"]) = "a"
-
-
StringUtils
.join(["a", "b", "c"], null) = "abc"
-
-
StringUtils
.join([null, "", "a"], ',', 0, 3) = ",,a"
-
StringUtils
.join(["a", "b", "c"], "--", 0, 3) = "a--b--c"
-
-
StringUtils
.join(["a", "b", "c"], "--", 1, 3) = "b--c"
-
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)
-
StringUtils
.substring("abcdefg", 0) = "abcdefg"
-
StringUtils
.substring("abcdefg", 2) = "cdefg"
-
StringUtils
.substring("abcdefg", 4) = "efg"
-
-
StringUtils
.substring("abcdefg", -2) = "fg"
-
StringUtils
.substring("abcdefg", -4) = "defg"
-
-
-
StringUtils
.substringAfter("abc", "a") = "bc"
-
StringUtils
.substringAfter("abcba", "b") = "cba"
-
StringUtils
.substringAfter("abc", "c") = ""
-
-
-
StringUtils
.substringBeforeLast("abcba", "b") = "abc"
-
StringUtils
.substringBeforeLast("abc", "c") = "ab"
-
StringUtils
.substringBeforeLast("a", "a") = ""
-
StringUtils
.substringBeforeLast("a", "z") = "a"
-
-
-
StringUtils
.substringAfterLast("abc", "a") = "bc"
-
StringUtils
.substringAfterLast("abcba", "b") = "a"
-
StringUtils
.substringAfterLast("abc", "c") = ""
-
-
StringUtils
.substringBetween("tagabctag", null) = null
-
StringUtils
.substringBetween("tagabctag", "") = ""
-
StringUtils
.substringBetween("tagabctag", "tag") = "abc"
截取分析圖:
截取分析圖
-
-
-
StringUtils
.substring("abc", -2, -1) = "b"
-
-
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)
-
-
StringUtils
.difference("", "abc") = "abc"
-
StringUtils
.difference("abc", "") = ""
-
StringUtils
.difference("abc", "abc") = ""
-
StringUtils
.difference("abc", "ab") = ""
-
StringUtils
.difference("ab", "abxyz") = "xyz"
-
StringUtils
.difference("abcde", "abxyz") = "xyz"
-
StringUtils
.difference("abcde", "xyz") = "xyz"
圖片理解:
差別圖片
縮短省略函數
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,則拋出異常。
-
-
StringUtils
.abbreviate("abcdefg", 6) = "abc..."
-
StringUtils
.abbreviate("abcdefg", 7) = "abcdefg"
-
StringUtils
.abbreviate("abcdefg", 8) = "abcdefg"
-
StringUtils
.abbreviate("abcdefg", 4) = "a..."
-
StringUtils
.abbreviate("abcdefg", 3) = IllegalArgumentException
匹配計數函數
countMatches(CharSequence str,char ch)
-
StringUtils
.countMatches("abba", 0) = 0
-
StringUtils
.countMatches("abba", 'a') = 2
-
StringUtils
.countMatches("abba", 'b') = 2
-
StringUtils
.countMatches("abba", 'x') = 0
刪除空白函數
trim(String str)
trimToEmpty(String str)
trimToNull(String str)
deleteWhitespace(String str)
-
StringUtils
.trim(" ") = ""
-
StringUtils
.trim("abc") = "abc"
-
StringUtils
.trim(" abc ") = "abc"
-
-
StringUtils
.trimToNull(" ") = null
-
StringUtils
.trimToNull("abc") = "abc"
-
StringUtils
.trimToNull(" abc ") = "abc"
-
StringUtils
.trimToEmpty(" ") = ""
-
StringUtils
.trimToEmpty("abc") = "abc"
-
StringUtils
.trimToEmpty(" abc ") = "abc"
-注意這二者的區別。
-
StringUtils
.deleteWhitespace("") = ""
-
StringUtils
.deleteWhitespace("abc") = "abc"
-
StringUtils
.deleteWhitespace(" ab c ") = "abc"
判斷是否相等函數
equals(CharSequence cs1,CharSequence cs2)
equalsIgnoreCase(CharSequence str1, CharSequence str2)
-
StringUtils
.equals("abc", null) = false
-
StringUtils
.equals("abc", "abc") = true
-
StringUtils
.equals("abc", "ABC") = false
-
-
StringUtils
.equalsIgnoreCase("abc", null) = false
-
StringUtils
.equalsIgnoreCase("abc", "abc") = true
-
StringUtils
.equalsIgnoreCase("abc", "ABC") = true
默認字符函數
defaultString(String str)
defaultString(String str,String defaultStr)
-
StringUtils
.defaultString("") = ""
-
StringUtils
.defaultString("bat") = "bat"
-
StringUtils
.defaultString("", "NULL") = ""
-
-
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)
-
StringUtils
.leftPad("bat", 3) = "bat"
-
-
StringUtils
.leftPad("bat", 5) = " bat"
-
StringUtils
.leftPad("bat", 1) = "bat"
-
StringUtils
.leftPad("bat", -1) = "bat"
-
-
StringUtils
.leftPad("bat", 3, 'z') = "bat"
-
StringUtils
.leftPad("bat", 5, 'z') = "zzbat"
-
StringUtils
.leftPad("bat", 1, 'z') = "bat"
-
StringUtils
.leftPad("bat", -1, 'z') = "bat"
-
-
StringUtils
.center("ab", -1) = "ab"
-
StringUtils
.center("ab", 4) = " ab "
-
StringUtils
.center("abcd", 2) = "abcd"
-
StringUtils
.center("a", 4) = " a
-
-
StringUtils
.center("abcd", 2, ' ') = "abcd"
-
StringUtils
.center("a", 4, ' ') = " a "
-
StringUtils
.center("a", 4, 'y') = "yayy"
-
-
StringUtils
.repeat("a", 3) = "aaa"
-
StringUtils
.repeat("ab", 2) = "abab"
-
StringUtils
.repeat("a", -2) = ""
-
-
StringUtils
.repeat("", "x", 3) = "xxx"
-
StringUtils
.repeat("?", ", ", 3) = "?, ?, ?"
-
-
-
StringUtils
.appendIfMissing("abcxyz", "xyz", "mno") = "abcxyz"
-
StringUtils
.appendIfMissing("abcmno", "xyz", "mno") = "abcmno"
-
StringUtils
.appendIfMissing("abcXYZ", "xyz", "mno") = "abcXYZxyz"
-
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)
- 例子
-
-
StringUtils
.containsOnly("ab", "") = false
-
StringUtils
.containsOnly("abab", "abc") = true
-
StringUtils
.containsOnly("ab1", "abc") = false
-
StringUtils
.containsOnly("abz", "abc") = false
-
-
-
StringUtils
.containsNone("abab", 'xyz') = true
-
StringUtils
.containsNone("ab1", 'xyz') = true
-
StringUtils
.containsNone("abz", 'xyz') = false
-
-
-
StringUtils
.startsWith("abcdef", "abc") = true
-
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)
-
-
StringUtils
.indexOf("aabaabaa", "a") = 0
-
StringUtils
.indexOf("aabaabaa", "b") = 2
-
StringUtils
.indexOf("aabaabaa", "ab") = 1
-
-
-
StringUtils
.indexOf("aabaabaa", "a", 0) = 0
-
StringUtils
.indexOf("aabaabaa", "b", 0) = 2
-
StringUtils
.indexOf("aabaabaa", "ab", 0) = 1
-
StringUtils
.indexOf("aabaabaa", "b", 3) = 5
-
StringUtils
.indexOf("aabaabaa", "b", 9) = -1
-
-
StringUtils
.lastIndexOf("aabaabaa", 'a') = 7
-
StringUtils
.lastIndexOf("aabaabaa", 'b') = 5
-
-
StringUtils
.lastIndexOfIgnoreCase("aabaabaa", "A", 8) = 7
-
StringUtils
.lastIndexOfIgnoreCase("aabaabaa", "B", 8) = 5
-
StringUtils
.lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
-
StringUtils
.lastIndexOfIgnoreCase("aabaabaa", "B", 9) = 5