Detailed explanation of all methods of StringUtils tool class & left padding (leftPad), right padding (rightPad), left and right padding (center)

StringUtils is a string processing tool class provided in Apache Commons Lang, which contains a wealth of string processing methods. All methods in the StringUtils class are described in detail below:

  1. isEmpty(CharSequence str) determines whether the character sequence str is empty, and returns true if it is null or has a length of 0.
  2. isNotEmpty has the opposite function of the isEmpty method and returns true when the character sequence is not empty.
  3. isAnyEmpty(CharSequence… css) determines whether multiple character sequences are empty, and returns true if one is empty.
  4. isNoneEmpty(CharSequence… css) is the opposite function of the isAnyEmpty method. It returns true when all character sequences are not empty.
  5. isBlank(CharSequence str) determines whether the character sequence str is empty or contains only spaces. If it is null, has a length of 0, or contains only spaces, it returns true.
  6. isNotBlank(CharSequence str) Contrary to the isBlank method, it returns true when the character sequence is not empty and does not contain only spaces.
  7. trim(CharSequence str) removes spaces at the beginning and end of the character sequence str.
  8. trimToNull(CharSequence str) If the result after trim(str) is an empty string, null is returned; otherwise, a string with spaces removed is returned.
  9. trimToEmpty(CharSequence str) If the result after trim(str) is an empty string, it will return an empty string, otherwise it will return a string with spaces removed.
  10. equals(CharSequence cs1, CharSequence cs2) Compares two character sequences cs1 and cs2 for equality, and returns true if both are null.
  11. equalsIgnoreCase(CharSequence str1, CharSequence str2) Compares two character sequences str1 and str2 for equality, ignoring case, and returns true if both are null.
  12. indexOf(CharSequence seq, int searchChar) finds the position where the search character searchChar first appears in the character sequence seq, and returns -1 if not found.
  13. indexOf(CharSequence seq, CharSequence searchSeq) finds the position where the substring searchSeq first appears in the character sequence seq, and returns -1 if not found.
  14. lastIndexOf(CharSequence seq, int searchChar) finds the position where the search character searchChar last appears in the character sequence seq, and returns -1 if not found.
  15. lastIndexOf(CharSequence seq, CharSequence searchSeq) finds the position where the substring searchSeq last appeared in the character sequence seq, and returns -1 if not found.
  16. contains(CharSequence seq, CharSequence searchSeq) determines whether the character sequence seq contains the substring searchSeq.
  17. containsIgnoreCase(CharSequence str1, CharSequence str2) determines whether the character sequence str1 contains the string str2, ignoring case.
  18. indexOfAny(CharSequence cs, char… searchChars) searches the character sequence cs for the first occurrence of any character in the character array searchChars, and returns -1 if not found.
  19. indexOfAny(CharSequence cs, String searchChars) has the same function as indexOfAny(CharSequence cs, char… searchChars), except that the search characters become strings.
  20. containsAny(CharSequence cs, char… searchChars) determines whether the character sequence cs contains any character in the character array searchChars.
  21. containsAny(CharSequence cs, CharSequence searchChars) has the same function as containsAny(CharSequence cs, char… searchChars), except that the search characters become character sequences.
  22. containsAnyIgnoreCase(CharSequence cs, CharSequence… searchCharSequences) determines whether the character sequence cs contains any string in the character sequence array searchCharSequences, ignoring case.
  23. containsNone(CharSequence cs, char… searchChars) determines whether the character sequence cs does not contain any character in the character array searchChars.
  24. containsNone(CharSequence cs, String invalidChars) has the same function as containsNone(CharSequence cs, char… searchChars), except that the search characters become strings.
  25. indexOfAnyBut(CharSequence str, char… searchChars) searches for the first occurrence of the character sequence str except the characters in the character array searchChars. If it is not found, it returns -1.
  26. indexOfIgnoreCase(CharSequence str, CharSequence searchStr) searches for the first occurrence of the string searchStr in the character sequence str, ignoring case, and returns -1 if not found.
  27. lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr) finds the last occurrence of the string searchStr in the character sequence str, ignoring case, and returns -1 if not found.
  28. substring(CharSequence str, int start) intercepts the substring of character sequence str from start to end.
  29. substring(CharSequence str, int start, int end) intercepts the substring of character sequence str starting from start and ending at end.
  30. left(CharSequence str, int len) intercepts len characters from the left side of the character sequence str. If the length of str is less than len, the entire character sequence is returned.
  31. right(CharSequence str, int len) intercepts len characters from the right side of the character sequence str. If the length of str is less than len, the entire character sequence is returned.
  32. mid(CharSequence str, int pos, int len) intercepts len characters starting from the pos character in the character sequence str.
  33. substringBefore(CharSequence str, CharSequence separator) Gets the substring before the first occurrence of the separator in the character sequence str. If the separator is not found, the entire string is returned.
  34. substringAfter(CharSequence str, CharSequence separator) Gets the substring after the first occurrence of the separator in the character sequence str. If the separator is not found, an empty string is returned.
  35. substringBeforeLast(CharSequence str, CharSequence separator) has the same function as substringBefore(CharSequence str, CharSequence separator), except that it finds the last occurrence of the separator.
  36. substringAfterLast(CharSequence str, CharSequence separator) has the same function as substringAfter(CharSequence str, CharSequence separator), except that it finds the last occurrence of the separator.
  37. substringBetween(CharSequence str, CharSequence tag) Gets the substring between two tags in the character sequence str, and returns null if not found.
  38. substringBetween(CharSequence str, CharSequence open, CharSequence close) Gets the substring between open and close in character sequence str, and returns null if not found.
  39. getNestedString(CharSequence str, CharSequence tag) has the same function as substringBetween(CharSequence str, CharSequence tag), except that the tag is replaced by matching prefix and suffix.
  40. countMatches(CharSequence str, CharSequence sub) Counts the number of matching substrings sub in the character sequence str.
  41. replaceOnce(CharSequence text, CharSequence search, CharSequence replacement) replaces the first occurrence of substring search with string replacement in character sequence text.
  42. replace(CharSequence text, CharSequence search, CharSequence replacement) Replaces all matching substrings search in the character sequence text with string replacement.
  43. replaceChars(CharSequence str, char searchChar, char replaceChar) Replaces all matching searchChars in the character sequence str with replaceChar.
  44. overlay(CharSequence str, CharSequence overlay, int start, int end) overlays the character sequence str from start to end with the character sequence overlay.
  45. leftPad(CharSequence str, int size, char padChar) uses character padChar to pad the left side of character sequence str to the specified length size.
  46. rightPad(CharSequence str, int size, char padChar) uses character padChar to pad the right side of character sequence str to the specified length size.
  47. center(CharSequence str, int size, char padChar) pads characters padChar at both ends of character sequence str so that its length is size.
  48. repeat(CharSequence str, int repeat) Repeats the character sequence str repeat times.
  49. reverse(CharSequence str) reverses the character sequence str.
  50. abbreviate(CharSequence str, int maxWidth) Abbreviate the character sequence str so that its length does not exceed maxWidth.
  51. wrap(CharSequence str, int wrapLength) wraps the character sequence str according to the length of wrapLength.
  52. wrap(CharSequence str, String wrapMarker, int wrapLength) Marks the character sequence str with wrapMarker and wraps it according to the length of wrapLength.

Summary: The StringUtils class provides a wealth of string processing methods, which can easily operate and process strings. Among them, commonly used methods include: isEmpty, isBlank, trim, equals, contains, indexOf, substring, replace, etc.



Left padding (leftPad), right padding (rightPad), left and right padding (center) tool methods

		//左侧补齐 第一个参数:原始字符串,第二个参数:字符串的长度,第三个是补充的字符串
        String newStr1 = StringUtils.leftPad("oldStr1", 10, "*");
        System.err.println(newStr1);//***oldStr1
        //获取字符串左侧指定长度的字符串,第一个参数:原字符串,第二个参数:取左侧字符串的长度
        String newStr2 = StringUtils.left("oldStr3", 5);
        System.err.println(newStr2);//oldSt
        //左侧补齐,默认使用空格补齐,第一个参数:原字符串,第二个参数:字符串总长度,不足用空格补全
        String newStr3 = StringUtils.leftPad("oldStr4", 10);
        System.err.println(newStr3);//        oldStr4
        //左侧补齐 第一个参数:原始字符串,第二个参数:字符串的长度,第三个是补充的字符串
        String newStr4 = StringUtils.center("oldStr1", 10, "*");
        System.err.println(newStr4);//*oldStr1**

Supongo que te gusta

Origin blog.csdn.net/qq_43842093/article/details/135188707
Recomendado
Clasificación