[Switch] Java StringUtils common methods

The operation object of the StringUtils method is an object of type Java.lang.String, which is complementary to the operation method of the String type provided by the JDK, and is null-safe (that is, if the input parameter String is null, it will not throw NullPointerException, but instead Corresponding treatment, for example, if the input is null, the return is also null, etc., you can check the source code for details).

In addition to the constructor, there are more than 130 methods in StringUtils, and they are all static, so we can call StringUtils.xxx () like this

The following is a brief introduction to some common methods:

1. public static boolean isEmpty (String str) to 
   determine whether a string is empty, the standard for empty is str == null or str.length () == 0 
   The following is an example:

StringUtils.isEmpty (null) = true
StringUtils.isEmpty ("") = true 
StringUtils.isEmpty ("") = false // Note that spaces are not empty in
StringUtils StringUtils.isEmpty ("") = false
StringUtils.isEmpty ( "bob") = false
StringUtils.isEmpty ("bob") = false
2. public static boolean isNotEmpty (String str) to 
   determine whether a string is not empty, equal to! isEmpty (String str) 
   The following is an example:

StringUtils.isNotEmpty (null) = false
StringUtils.isNotEmpty ("") = false
StringUtils.isNotEmpty ("") = true
StringUtils.isNotEmpty ("") = true
StringUtils.isNotEmpty ("bob") = true
StringUtils.isNotEmpty (" bob ") = true
3. public static boolean isBlank (String str) to 
   determine whether a string is empty or has a length of 0 or consists of whitespace (whitespace)
   The following is an example:

StringUtils.isBlank (null) = true
StringUtils.isBlank ("") = true
StringUtils.isBlank ("") = true
StringUtils.isBlank ("") = true
StringUtils.isBlank ("\ t \ n \ f \ r") = true // For tabs, line breaks, page breaks and carriage returns

StringUtils.isBlank () // recognized as blank characters
StringUtils.isBlank ("\ b") = false // "\ b" is a word Boundary character
StringUtils.isBlank ("bob") = false
StringUtils.isBlank ("bob") = false
4. public static boolean isNotBlank (String str) 
   determines whether a string is not empty and the length is not 0 and is not caused by white space ( whitespace), equal to! isBlank (String str) 
   The following is an example:

StringUtils.isNotBlank (null) = false
StringUtils.isNotBlank ("") = false
StringUtils.isNotBlank ("") = false
StringUtils.isNotBlank ("") = false
StringUtils.isNotBlank ("\ t \ n \ f \ r") = false
StringUtils.isNotBlank ("\ b") = true
StringUtils.isNotBlank ("bob") = true
StringUtils.isNotBlank ("bob") = true
5. public static String trim (String str) 
   remove the control characters at both ends of the string (control characters, char <= 32), if the input is null then return null. 
   Here is an example:

StringUtils.trim (null) = null
StringUtils.trim ("") = ""
StringUtils.trim ("") = ""
StringUtils.trim ("\ b \ t \ n \ f \ r") = ""
StringUtils. trim ("\ n \ tss \ b") = "ss"
StringUtils.trim ("d d dd") = "d d dd"
StringUtils.trim ("dd") = "dd"
StringUtils.trim ("dd") = "dd"
6. public static String trimToNull (String str) 
   Remove the control characters (control characters, char <= 32) at both ends of the string, if it becomes null or "", then return null. 
   The following is an example:

StringUtils.trimToNull (null) = null
StringUtils.trimToNull ("") = null
StringUtils.trimToNull ("") = null
StringUtils.trimToNull ("\ b \ t \ n \ f \ r") = null
StringUtils.trimToNull (" \ n \ tss \ b ") =" ss "
StringUtils.trimToNull (" d d dd ") =" d d dd "
StringUtils.trimToNull (" dd ") =" dd "
StringUtils.trimToNull (" dd ") =" dd "
7. public static String trimToEmpty (String str) 
   remove the control characters (control characters, char <= 32) at both ends of the string, if it becomes null or "", then return "" 
   The following is an example:

StringUtils.trimToEmpty (null) = ""
StringUtils.trimToEmpty ("") = ""
StringUtils.trimToEmpty ("") = ""
StringUtils.trimToEmpty ("\ b \ t \ n \ f \ r") = ""
StringUtils .trimToEmpty ("\ n \ tss \ b") = "ss"
StringUtils.trimToEmpty ("d d dd") = "d d dd"
StringUtils.trimToEmpty ("dd") = "dd"
StringUtils.trimToEmpty ("dd") = "dd"
8. public static String strip (String str) 
   remove the whitespace at both ends of the string, if the input is null, return null. 
   The following is an example (note the difference with trim ()):

StringUtils.strip (null) = null
StringUtils.strip ("") = ""
StringUtils.strip ("") = ""
StringUtils.strip ("\ b \ t \ n \ f \ r") = "\ b"
StringUtils.strip ("\ n \ tss \ b") = "ss \ b"
StringUtils.strip ("d d dd") = "d d dd"
StringUtils.strip ("dd") = "dd"
StringUtils.strip (" dd ") =" dd "
9. public static String stripToNull (String str) 
   remove the white space at both ends of the string (whitespace), if it becomes null or" ", then return null 
   The following is an example (note the difference between trimToNull () ):

StringUtils.stripToNull (null) = null
StringUtils.stripToNull ("") = null
StringUtils.stripToNull ("") = null
StringUtils.stripToNull ("\ b \ t \ n \ f \ r") = "\ b"
StringUtils. stripToNull ("\ n \ tss \ b") = "ss \ b"
StringUtils.stripToNull ("d d dd") = "d d dd"
StringUtils.stripToNull ("dd") = "dd"
StringUtils.stripToNull ("dd" ) = "dd"
10. public static String stripToEmpty (String str) 
    remove the whitespace at both ends of the string, if it becomes null or "", then return "" 
    The following is an example (note the difference with trimToEmpty ()) :

StringUtils.stripToNull (null) = ""
StringUtils.stripToNull ("") = ""
StringUtils.stripToNull ("") = ""
StringUtils.stripToNull ("\ b \ t \ n \ f \ r") = "\ b "
StringUtils.stripToNull (" \ n \ tss \ b ") =" ss \ b "
StringUtils.stripToNull (" d d dd ") =" d d dd "
StringUtils.stripToNull (" dd ") =" dd "
StringUtils.stripToNull ( "dd") = "dd" The
following method only introduces its function, no more examples:
11. public static String strip (String str, String stripChars) 
   Remove the characters in stripChars at both ends of str.
   If str is null or equal to "", it returns itself;
   if stripChars is null or ""

12. public static String stripStart (String str, String stripChars) is 
    similar to 11, remove the characters in stripChars in front of str.

13. Public static String stripEnd (String str, String stripChars) 
    Similar to 11, remove the characters in stripChars at the end of str.

14. public static String [] stripAll (String [] strs) 
    strip (String str) each string in the string array, and then return.
    If strs is null or strs length is 0, then return strs itself

15. public static String [] stripAll (String [] strs, String stripChars) 
    performs strip (String str, String stripChars) on each string in the string array, and then returns.
    If strs is null or strs length is 0, then return strs itself

16. Public static boolean equals (String str1, String str2) 
    compares whether two strings are equal, if both are empty, they are also considered equal.

17. public static boolean equalsIgnoreCase (String str1, String str2) 
    compares two strings for equality, not case sensitive, if both are empty, they are also considered equal.

18. public static int indexOf (String str, char searchChar) 
    returns the position where the character searchChar first appears in the string str.
    Return -1 if searchChar does not appear in str, or -1
    if str is null or ""

19. public static int indexOf (String str, char searchChar, int startPos) 
    returns the position where the character searchChar first appears in the string str starting from startPos.
    Returns -1
    if searchChar does not appear in str from startPos, or -1 if str is null or ""

20. public static int indexOf (String str, String searchStr) 
    returns the position where the string searchStr first appears in the string str.
    If str is null or searchStr is null then -1 is returned,
    if searchStr is "" and str is not null, then 0 is returned,
    if searchStr is not in str, then -1 is returned

21. public static int ordinalIndexOf (String str, String searchStr, int ordinal) 
    returns the position of the string searchStr in the string str ordinal.
    If str = null or searchStr = null or ordinal <= 0, then return -1
    for example (* represents any string):

StringUtils.ordinalIndexOf (null, *, *) = -1
StringUtils.ordinalIndexOf (* null *) = -1
StringUtils.ordinalIndexOf ( "", "") = 0
StringUtils.ordinalIndexOf ( "aabaabaa", "a "1) = 0
StringUtils.ordinalIndexOf (" aabaabaa "," a ", 2) = 1
StringUtils.ordinalIndexOf (" aabaabaa "," B ", 1) = 2
StringUtils.ordinalIndexOf (" aabaabaa "," b " 2) = 5
StringUtils.ordinalIndexOf ( "aabaabaa", "from", 1) = 1
StringUtils.ordinalIndexOf ( "aabaabaa", "from", 2) = 4
StringUtils.ordinalIndexOf ( "aabaabaa", "b", 1) = -1
StringUtils.ordinalIndexOf ( "aabaabaa", "", 1) = 0
StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0
22. public static int indexOf(String str, String searchStr, int startPos) 
    Returns the position where the string searchStr first appears in the string str starting from startPos.
    Example (* represents any string):

StringUtils.indexOf(null, *, *) = -1
StringUtils.indexOf(*, null, *) = -1
StringUtils.indexOf("", "", 0) = 0
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.indexOf("aabaabaa", "b", -1) = 2
StringUtils.indexOf("aabaabaa", "", 2) = 2
StringUtils.indexOf("abc", "", 9) = 3
23. public static int lastIndexOf(String str, char searchChar) 
    基本原理同18

24. The
    basic principle of public static int lastIndexOf (String str, char searchChar, int startPos) is the  same as 19

25. The
    basic principle of public static int lastIndexOf (String str, String searchStr) is the  same as 20

26. public static int lastIndexOf(String str, String searchStr, int startPos)

 

From: https://www.cnblogs.com/xxl910/p/12664777.html

Guess you like

Origin www.cnblogs.com/sanxiao/p/12699679.html