1, Conversion
the methods involved in case conversion and string case judgment in StringUtils are as follows:
1)StringUtils.capitalize(String str) 2)StringUtils.uncapitalize(String str) 3)StringUtils.upperCase(String str) 4)StringUtils.upperCase(String str,Locale locale) 5)StringUtils.lowerCase(String str) 6)StringUtils.lowerCase(String str,Locale locale) 7)StringUtils.swapCase(String str) 8)StringUtils.isAllUpperCase(CharSequence cs) 9)StringUtils.isAllLowerCase(CharSequence cs)
(1) case conversion of string initial
StringUtils.capitalize(null)); // null (note that no exception will be reported here) StringUtils.capitalize("china")); // China (initial to capital) StringUtils.uncapitalize(null)); // null StringUtils.uncapitalize("CHINA")); // cHINA (initial to lowercase)
(2) overall case conversion of string
StringUtils.upperCase(null)); // null StringUtils.upperCase("china")); // CHINA (all capitalized) StringUtils.upperCase("china", Locale.ENGLISH)); // CHINA (convert to uppercase according to the specified rules) StringUtils.lowerCase(null)); // null StringUtils.lowerCase("CHINA")); // china (all converted to lowercase) StringUtils.lowerCase("CHINA", Locale.ENGLISH)); // china (convert to lowercase according to the specified conversion rules)
(3) string case exchange
StringUtils.swapCase(null)); // null StringUtils.swapCase("chINA")); // CHina
(4) judge whether the string is all uppercase or lowercase (empty or blank characters are false)
StringUtils.isAllUpperCase(null)); // false StringUtils.isAllUpperCase("")); // false StringUtils.isAllUpperCase(" ")); // false StringUtils.isAllUpperCase("CHINA")); // true StringUtils.isAllLowerCase(null)); // false StringUtils.isAllLowerCase("")); // false StringUtils.isAllLowerCase(" ")); // false StringUtils.isAllLowerCase("china")); // true
2, Remove
remove the matching character or character sequence from the string. If the character or character sequence to be removed does not exist in the string, that is, there is no match, it will not be removed
1)StringUtils.remove(String str, char remove) 2)StringUtils.remove(String str, String remove) 3)StringUtils.removeStart(String str, String remove) 4)StringUtils.removeStartIgnoreCase(String str, String remove) 5)StringUtils.removeEnd(String str, String remove) 6)StringUtils.removeEndIgnoreCase(String str, String remove) 7)StringUtils.deleteWhitespace(String str)
(1) remove single characters
StringUtils.remove(null, 'a')); // Null (note the null behavior here and next) StringUtils.remove('china', null) // china StringUtils.remove("china", 'i')); // chna StringUtils.remove("china", 'b')); // china (if the character to be removed does not exist, the original string is returned)
(2) removes the specified character sequence
StringUtils.remove("china", "in")); // cha StringUtils.remove("china", "nin")); // china
(3) remove the character sequence that matches the beginning
StringUtils.removeStart("china", "ch")); // ina StringUtils.removeStartIgnoreCase("china", "CHI")); // na (ignore case)
(4) remove the character sequence with matching end
StringUtils.removeEnd("china", "na")); // chi StringUtils.removeEndIgnoreCase("china", "NA")); // chi (ignore case)
(5) remove white space characters
StringUtils.deleteWhitespace(null)); //null StringUtils.deleteWhitespace(" c h i\tn\ra")); // china
3, Replace
there are several common replacement methods in StringUtils
1)replace(String text, String searchString, String replacement) 2)replace(String text, String searchString, String replacement, int max) 3)replaceChars(String str, char searchChar, char replaceChar) 4)replaceChars(String str, String searchChars, String replaceChars) 5)replaceOnce(String text, String searchString, String replacement) 6)overlay(String str,String overlay,int start,int end) 7)replaceEach(String text, String[] searchList, String[] replacementList) 8)replaceEachRepeatedly(String text, String[] searchList, String[]replacementList)
it should be noted that if the replaced string is null, or the replaced character or character sequence is null, or the replaced character or character sequence is null, the replacement will be ignored and the original string will be returned
(1) replace a single character or character sequence
(a) replace method
the replace method can replace a single character sequence
StringUtils.replace("china", null, "z")); // china (the replaced character sequence here is null, so the replacement will be ignored and the original string will be returned) StringUtils.replace("china", "c", null)); // china (the replacement character sequence here is null, so the replacement is also ignored and the original string is returned) StringUtils.replace("china", "a", "ese")); // chinese StringUtils.replace("china", "a", "")); // chin
the replace method can also specify the maximum number of replacements
StringUtils.replace("aabaaa", "a", "z", 0)); // aabaaa (0 means that the number of replacements is 0, that is, no replacement) StringUtils.replace("aabaaa", "a", "z", 1)); // zabaaa (1 means to replace 1 at most) StringUtils.replace("aabaaa", "a", "z", 2)); // zzbaaa (2 means to replace 2 at most) StringUtils.replace("aabaaa", "a", "z", 3)); // zzbzaa (3 means to replace 3 at most) StringUtils.replace("aabaaa", "a", "z", -1)); // zzbzaa (-1 means replace all)
(b) replacecharts method
the replaceChars method can replace a single character or a single character sequence
StringUtils.replaceChars("china", 'a', 'z')); // chinz StringUtils.replaceChars("china", "a", "z")); // chinz
(c) replaceOnce method
the replaceOnce method will only replace once, that is, only the first character sequence to be replaced will be replaced, and even if there is a matching character sequence, it will not be replaced
StringUtils.replaceOnce("abaa", "a", "z")); // zbaa
(d) overlay method
overlay(String str,String overlay,int start,int end) method can replace the character sequence at the specified position, starting from (including) the start index to the end-1 index
StringUtils.overlay("abcdef", "zzzz", 2, 4)); // abzzzzef
there are some special circumstances:
1) if the start index start is less than the end index end, end will be used as the start index and start as the end index
StringUtils.overlay("abcdef", "zzzz", 4, 2)); // abzzzzef StringUtils.overlay("abcdef", "zzzz", 4, 3)); // abczzzzef StringUtils.overlay("abcdef", "zzzz", 4, 4)); // abcdzzzzef StringUtils.overlay("abcdef", "zzzz", 4, 5)); // abcdzzzzf
2) when the start index start is negative, start will be treated as 0
StringUtils.overlay("abcdef", "zzzz", -1, 2)); // abcdzz StringUtils.overlay("abcdef", "zzzz", -2, -3)); // zzzzabcdef
3) the end index end is greater than the length of the original string. In this case, end will be treated as the length of the original string
StringUtils.overlay("abcdef", "zzzz", 8, 10)); // abcdefzzzz
(2) replace multiple character sequences at the same time
(a) replaceEach method
replaceEach(String text, String[] searchList, String[] replacementList) method can replace multiple character sequences at the same time, but the number of character sequences replaced and replaced should correspond, otherwise it will report IllegalArgumentException
StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z" })); // xhinz (replace ch and a with x and Z, respectively) StringUtils.replaceEach("china", null, new String[] { "x", "z" })); // china (null exists, no replacement) StringUtils.replaceEach("china", new String[] { "ch", "a" }, new String[] { "x", "z", "y" })); // IllegalArgumentException (the number of replaced and replaced does not correspond)
(b) replaceEachRepeatedly method
replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) method can be replaced circularly. See the following example for details:
StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "z" })); // zhina (c is replaced by x, and x is replaced by z)
but if the replacement is an endless loop, it will report IllegalStateException:
StringUtils.replaceEachRepeatedly("china", new String[] { "c", "x" }, new String[] { "x", "c" })); // IllegalStateException (c is replaced by x, and x is replaced by c)
4, Reverse
the methods of inversion in StringUtils are as follows:
1)reverse(String str) 2)reverseDelimited(String str, char separatorChar)
(1) simple inversion
reverse(String str) method
StringUtils.reverse("china")); // anihc
(2) reverse according to the specified separator, and the characters between separators are not reversed
StringUtils.reverseDelimited("china", ',')); // china StringUtils.reverseDelimited("cxhinxa", 'x')); // axhinxz StringUtils.reverseDelimited("c.hin.a", '.')); // a.hin.c StringUtils.reverseDelimited("c.hina", '.')); // hina.c
Additional supplement(There may be duplication)
Is isEmpty(String str) empty and the space character false
Whether isNotEmpty(String str) is non empty and the space character is true
Whether isBlank(String str) is empty and the space character is true
Is isNotBlank(String str) non empty and the space character false
trim(String str) removes the control characters at both ends of the string. Empty string and null return null
trimToEmpty(String str) removes the control characters at both ends of the string. Empty string and null return ""
stripToNull(String str) removes the blank characters at both ends of the string. Empty string and null return null
stripToEmpty(String str) removes the blank characters at both ends of the string, and returns "" for empty string and null
strip(String str, String stripChars) removes the characters in stripChars at both ends of str
StringUtils.strip("000000134_76539000","0")="134_76539"
stripStart (String str,String stripChars) removes the characters in stripChars at the front end of str
stripEnd (String str,String stripChars) removes the characters in stripChars at the back end of str
equals(String str1,String str2) compares whether two strings are equal. If both strings are empty, they are considered equal
Indexof (string str, char, searchChar) returns the position where searchChar first appears in the string. If it is not found, it returns - 1. If str is null or "", it also returns - 1
Indexof (string str, char, searchChar, int startPos) returns the position where the character searchChar first appears in the string str starting from startPos.
Contains (string STR, char, searchChar) whether the str contains the character searchChar. If STR is null or searchChar is null, false will be returned.
StringUtils.contains("", "") = true
StringUtils.contains("dfg", "") = true
containsIgnoreCase(String str,String searchStr) whether the str contains the character searchChar is case insensitive
int indexOfAny(String str, char[] searchChars) finds the position where the character in the character array searchChars first appears in the string str. Returns - 1 if none of the characters in the character array are in the string, or - 1 if the string is null or ''
subString(String str,int start) starts from start and contains the character start to get the substring of string str. if start is a negative number, it starts from the following. Returns STR itself if it is null or ''
Substring before (string str, string separator) gets the substring before the first occurrence of the string separator. Does not contain that character, and returns itself if str is null or ''.
subStringAfter(String str,String separator) gets the substring after the first occurrence of the string separator, which does not contain that character. If str is null or "", it returns itself
subString(String str,int start,int end) is the same as above
left(String str,int len) gets the substring of length len of string str from the left. If str is null or "", it returns itself. If len is less than 0, it returns ""
right(String str,int len) gets the string str, counting the substring of len length from the right
mid(String str,int pos,int len) gets the substring of string str starting from pos and Len length. If pos is less than 0, it is set to 0.
split(String str) splits the string into a string array, using the blank character as the separator. Null is returned if the string is null, and null is returned if the string is ""
split(String str,char c) splits strings according to char C
Join (object [] array) connects the elements in the array into a string and returns
Join (object [] array, char, c) concatenates the elements in the array into a string and returns it, along with the separator C
deleteWhitespace(String str) deletes all whitespace characters in the string, including escape characters
removeStart(String str,String remove) if the string str starts with remove, remove the start and return. Otherwise, return the original string
removeEnd(String str,String remove) if the string str ends with the string remove, remove the end and return. Otherwise, return the original string.
remove(String str,char remove) removes all parts of the string str that contain remove, and then returns
replace(String str,String reql,String with) replaces replace with with with all in the string text
replaceChars(String str,char old,char new) replaces the old character with the new character in the string
replaceChars(String str, String searchChars, String replaceChars) is a little special. Let's look at the following three examples first
StringUtils.replaceChars("asssdf","s","yyy")) = "ayyydf"
StringUtils.replaceChars("asdf","sd","y")) = "ayf"
StringUtils.replaceChars("assssddddf","sd","y"))= "ayyyyf"
Explanation: why does this result appear? The original replacement rule is like this. He takes the index of searchChars, goes to replaceChars to find the corresponding index, and then replaces it. How to say? For example, in the first example, the index of S is 0, and the character with index 0 corresponding to yyy is y. In the second example, the index of's' is 0 and the index of'd 'is 1. The character's' can find the' y 'corresponding to index 0, and D can't find the character with index' 1 ', so it is filtered out directly. Do you understand?
overlay(String str,String new,int start,int end) overwrites the string str from start to end with string new
chop(String str) removes the last character of the string, such as / r/n
Repeat (string STR, int repeat) repeats the string twice
rightPad(String str,int size,String padStr) is a string of size length. If it is not enough, use padStr to supplement it
leftPad(String str,int size,String padStr) is the same as above
center(String str,int size) generates a string with a length equal to size, and str is located at the center of the new string
swapCase(String str) converts uppercase to lowercase and lowercase to uppercase in a string
From: https://www.cnblogs.com/jmcui/p/7208383.html