開(kāi)心一笑
【男女朋友一起去逛街碑诉,
女朋友:哎喲,腳好酸哦侥锦。
男朋友很緊張:怎么了进栽?是不是踩到檸檬了?】
提出問(wèn)題
Land3的StringUtils類(lèi)如何使用恭垦?快毛??
解決問(wèn)題
StringUtils是工作中使用最頻繁的一個(gè)工具類(lèi)番挺,提供了大量豐富的字符串操作方法唠帝,下面是所有方法的一個(gè)藍(lán)圖:
判空函數(shù)
1)StringUtils.isEmpty(String str)
2)StringUtils.isNotEmpty(String str)
3)StringUtils.isBlank(String str)
4)StringUtils.isNotBlank(String str)
5)StringUtils.isAnyBlank(CharSequence... css)
6)StringUtils.isAnyEmpty(CharSequence... css)
7)StringUtils.isNoneBlank(CharSequence... css)
8)StringUtils.isNoneEmpty(CharSequence... css)
9)StringUtils.isWhitespace(CharSequence cs)
isNotEmpty = !isEmpty, isBlank同理;
-
容易忽略的玄柏;
StringUtils.isEmpty("") = true
-
isBlank和isEmpty區(qū)別:
System.out.println(StringUtils.isBlank(" "));//true System.out.println(StringUtils.isBlank(" "));//true System.out.println(StringUtils.isBlank("\n\t"));//true //區(qū)別 StringUtils.isEmpty(" ") = false
-
isAnyBlank和isAnyEmpty是多維判空襟衰,存在一個(gè)blank或者empty既true
StringUtils.isAnyBlank("", "bar","foo"); = true //注意這兩個(gè)區(qū)別 StringUtils.isAnyEmpty(" ", "bar") = false StringUtils.isAnyEmpty(" ", "bar") = true
-
isNoneBlank = !isAnyBlank;isNoneEmpty同理
public static boolean isNoneBlank(CharSequence... css) { return !isAnyBlank(css); }
-
isWhitespace判斷空白
StringUtils.isWhitespace(null) = false StringUtils.isWhitespace("") = true StringUtils.isWhitespace(" ") = true
大小寫(xiě)函數(shù)
StringUtils.capitalize(String str)
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)
注意點(diǎn):
-
capitalize首字母大寫(xiě)粪摘,upperCase全部轉(zhuǎn)化為大寫(xiě)瀑晒,swapCase大小寫(xiě)互轉(zhuǎn);
StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" //首字母轉(zhuǎn)為大寫(xiě) StringUtils.capitalize("cat") = "Cat" //全部轉(zhuǎn)為大寫(xiě) StringUtils.upperCase("aBc") = "ABC" //大小寫(xiě)互轉(zhuǎn) StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
-
isAllUpperCase是否全部大寫(xiě)徘意,isAllLowerCase是否全部小寫(xiě)
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
刪除函數(shù)
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" //刪除結(jié)尾匹配的字符串 StringUtils.removeEnd("www.domain.com", ".com") = "www.domain" //刪除結(jié)尾匹配的字符串,找都不到返回原字符串 StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com" //忽略大小寫(xiě)的 StringUtils.removeEndIgnoreCase("www.domain.com", ".COM") = "www.domain") //刪除所有空白(好用) StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace(" ab c ") = "abc"
替換函數(shù)
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" //數(shù)字就是替換個(gè)數(shù)苔悦,0代表不替換,1代表從開(kāi)始數(shù)起第一個(gè)映砖,-1代表全部替換 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的增強(qiáng)版间坐,搜索列表和替換長(zhǎng)度必須一致,否則報(bào)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"
-
replaceOnce只替換一次:
StringUtils.replaceOnce("aba", "a", "") = "ba" StringUtils.replaceOnce("aba", "a", "z") = "zba" StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte" //這是一個(gè)非常奇怪的函數(shù)邑退,本來(lái)自己料想結(jié)果應(yīng)該是“dcte”的竹宋,可是結(jié)果居然是tcte,而dcte再替換一次就是tcte了地技。 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"
反轉(zhuǎn)函數(shù)
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"
分隔合并函數(shù)
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"]
//0 或者負(fù)數(shù)代表沒(méi)有限制
StringUtils.split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"]
//分割字符串 ,可以設(shè)定得到數(shù)組的長(zhǎng)度蜈七,限定為2
StringUtils.split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
//null也可以作為分隔
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"]
//帶有限定長(zhǎng)度的分隔
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)
例:
//只有一個(gè)參數(shù)的join,簡(jiǎn)單合并在一起
StringUtils.join(["a", "b", "c"]) = "abc"
StringUtils.join([null, "", "a"]) = "a"
//null的話(huà)莫矗,就是把字符合并在一起
StringUtils.join(["a", "b", "c"], null) = "abc"
//從index為0到3合并飒硅,注意是排除3的
StringUtils.join([null, "", "a"], ',', 0, 3) = ",,a"
StringUtils.join(["a", "b", "c"], "--", 0, 3) = "a--b--c"
//從index為1到3合并砂缩,注意是排除3的
StringUtils.join(["a", "b", "c"], "--", 1, 3) = "b--c"
StringUtils.join(["a", "b", "c"], "--", 2, 3) = "c"
截取函數(shù)
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"
//start>0表示從左向右, start<0表示從右向左, start=0則從左第一位開(kāi)始
StringUtils.substring("abcdefg", -2) = "fg"
StringUtils.substring("abcdefg", -4) = "defg"
//從第二個(gè)參數(shù)字符串開(kāi)始截取,排除第二個(gè)字符串
StringUtils.substringAfter("abc", "a") = "bc"
StringUtils.substringAfter("abcba", "b") = "cba"
StringUtils.substringAfter("abc", "c") = ""
//從最后一個(gè)字母出現(xiàn)開(kāi)始截取
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"
截取分析圖:
// start>0&&end>0從左開(kāi)始(包括左)到右結(jié)束(不包括右),
//start<0&&end<0從右開(kāi)始(包括右),再向左數(shù)到end結(jié)束(包括end)
StringUtils.substring("abc", -2, -1) = "b"
//這個(gè)我至今還沒(méi)弄明白
StringUtils.substring("abc", -4, 2) = "ab"
相似度函數(shù)
一個(gè)字符串可以通過(guò)增加一個(gè)字符三娩,刪除一個(gè)字符庵芭,替換一個(gè)字符得到另外一個(gè)字符串,假設(shè)雀监,我們把從字符串A轉(zhuǎn)換成字符串B双吆,前面3種操作所執(zhí)行的最少次數(shù)稱(chēng)為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
差異函數(shù)
difference(String str1,String str2)
//在str1中尋找str2中沒(méi)有的的字符串会前,并返回
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"
圖片理解:
縮短省略函數(shù)
abbreviate(String str, int maxWidth)
abbreviate(String str, int offset, int maxWidth)
abbreviateMiddle(String str, String middle, int length)
注意:
字符串的長(zhǎng)度小于或等于最大長(zhǎng)度好乐,返回該字符串。
運(yùn)算規(guī)律(substring(str, 0, max-3) + "...")
-
如果最大長(zhǎng)度小于4瓦宜,則拋出異常蔚万。
//(substring(str, 0, 6-3) + "...") 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
匹配計(jì)數(shù)函數(shù)
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
刪除空白函數(shù)
trim(String str)
trimToEmpty(String str)
trimToNull(String str)
deleteWhitespace(String str)
StringUtils.trim(" ") = ""
StringUtils.trim("abc") = "abc"
StringUtils.trim(" abc ") = "abc"
//空的話(huà),返回null
StringUtils.trimToNull(" ") = null
StringUtils.trimToNull("abc") = "abc"
StringUtils.trimToNull(" abc ") = "abc"
StringUtils.trimToEmpty(" ") = ""
StringUtils.trimToEmpty("abc") = "abc"
StringUtils.trimToEmpty(" abc ") = "abc"
-注意這兩者的區(qū)別临庇。
StringUtils.deleteWhitespace("") = ""
StringUtils.deleteWhitespace("abc") = "abc"
StringUtils.deleteWhitespace(" ab c ") = "abc"
判斷是否相等函數(shù)
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
//忽略大小寫(xiě)
StringUtils.equalsIgnoreCase("abc", null) = false
StringUtils.equalsIgnoreCase("abc", "abc") = true
StringUtils.equalsIgnoreCase("abc", "ABC") = true
默認(rèn)字符函數(shù)
defaultString(String str)
defaultString(String str,String defaultStr)
StringUtils.defaultString("") = ""
StringUtils.defaultString("bat") = "bat"
StringUtils.defaultString("", "NULL") = ""
//如果第一個(gè)參數(shù)為空反璃,這返回第二個(gè)默認(rèn)參數(shù)
StringUtils.defaultString("bat", "NULL") = "bat"
填充居中函數(shù)
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"
//左填充,默認(rèn)填充空
StringUtils.leftPad("bat", 5) = " bat"
StringUtils.leftPad("bat", 1) = "bat"
StringUtils.leftPad("bat", -1) = "bat"
//左填充苔巨,填充字符為z
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
//居中版扩,最后一個(gè)參數(shù)是填充字符或字符串
StringUtils.center("abcd", 2, ' ') = "abcd"
StringUtils.center("a", 4, ' ') = " a "
StringUtils.center("a", 4, 'y') = "yayy"
//重復(fù)字符串,第二個(gè)參數(shù)是重復(fù)次數(shù)
StringUtils.repeat("a", 3) = "aaa"
StringUtils.repeat("ab", 2) = "abab"
StringUtils.repeat("a", -2) = ""
//重復(fù)字符串侄泽,第二個(gè)參數(shù)是分割符礁芦,第三個(gè)參數(shù)是重復(fù)次數(shù)
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"
是否包含函數(shù)
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) **
-
例子
//判斷字符串中所有字符,是否都是出自參數(shù)2中 StringUtils.containsOnly("ab", "") = false StringUtils.containsOnly("abab", "abc") = true StringUtils.containsOnly("ab1", "abc") = false StringUtils.containsOnly("abz", "abc") = false //判斷字符串中所有字符悼尾,都不在參數(shù)2中柿扣。 StringUtils.containsNone("abab", 'xyz') = true StringUtils.containsNone("ab1", 'xyz') = true StringUtils.containsNone("abz", 'xyz') = false //判斷字符串是否以第二個(gè)參數(shù)開(kāi)始 StringUtils.startsWith("abcdef", "abc") = true StringUtils.startsWith("ABCDEF", "abc") = false
索引下標(biāo)函數(shù)
indexOf(CharSequence seq,CharSequence searchSeq)
indexOf(CharSequence seq,CharSequence searchSeq,int startPos)
indexOfIgnoreCase/lastIndexOfIgnoreCase(CharSequence str,CharSequence searchStr)
lastIndexOf(CharSequence seq,int searchChar)
//返回第二個(gè)參數(shù)開(kāi)始出現(xiàn)的索引值
StringUtils.indexOf("aabaabaa", "a") = 0
StringUtils.indexOf("aabaabaa", "b") = 2
StringUtils.indexOf("aabaabaa", "ab") = 1
//從第三個(gè)參數(shù)索引開(kāi)始找起,返回第二個(gè)參數(shù)開(kāi)始出現(xiàn)的索引值
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
//返回第二個(gè)參數(shù)出現(xiàn)的最后一個(gè)索引值
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
讀書(shū)感悟
來(lái)自《我們仨》
- 我一個(gè)人闺魏,懷念我們仨未状。
- 從今往后,咱們只有死別析桥,再無(wú)生離.
- 我們這個(gè)家司草,很樸素;我們?nèi)齻€(gè)人泡仗,很單純埋虹。我們與世無(wú)求,與人無(wú)爭(zhēng)娩怎,只求相聚在一起搔课,相守在一起,各自做力所能及的事截亦。碰到困難爬泥,鐘書(shū)總和我一同承當(dāng)柬讨,困難就不復(fù)困難;還有個(gè)阿瑗相伴相助袍啡,不論什么苦澀艱辛的事踩官,都能變得甜潤(rùn)。我們稍有一點(diǎn)快樂(lè)葬馋,也會(huì)變得非陈衾穑快樂(lè)。所以我們仨是不尋常的遇合畴嘶。
- 兩年不見(jiàn),她好像已經(jīng)不認(rèn)識(shí)了集晚。她看見(jiàn)爸爸帶回的行李放在媽媽床邊窗悯,很不放心,猜疑地監(jiān)視著偷拔,晚飯后蒋院,圓圓對(duì)爸爸發(fā)話(huà)了。
“這是我的媽媽?zhuān)愕膵寢屧谀沁吜隆欺旧!彼s爸爸走。
鐘書(shū)很窩囊地笑說(shuō):“我倒問(wèn)問(wèn)你蛤签,是我先認(rèn)識(shí)你媽媽?zhuān)€是你先認(rèn)識(shí)辞友?”
“自然我先認(rèn)識(shí),我一生出來(lái)就認(rèn)識(shí)震肮,你是長(zhǎng)大了認(rèn)識(shí)的称龙。” - 惟有身處卑微的人戳晌,最有機(jī)緣看到世態(tài)人情的真相鲫尊。一個(gè)人不想攀高就不怕下跌,也不用傾軋排擠沦偎,可以保其天真疫向,成其自然,潛心一志完成自己能做的事豪嚎。
其他
如果有帶給你一絲絲小快樂(lè)搔驼,就讓快樂(lè)繼續(xù)傳遞下去,歡迎轉(zhuǎn)載疙渣,點(diǎn)贊匙奴,頂,歡迎留下寶貴的意見(jiàn)妄荔,多謝支持泼菌!