Usage of StringUtils in the Common-lang Package

Source: Internet
Author: User

Usage of StringUtils in the Common-lang Package

The operation object of the StringUtils method is java. lang. string-type objects are complementary to the String-type operation method provided by JDK, and are null safe (that is, if the input parameter String is null, NullPointerException is not thrown, but corresponding processing is performed, for example, if the input is null, the return value is also null. You can view the source code ).
In addition to the constringutils, there are more than 130 methods in StringUtils and they are all static, so we can call StringUtils. xxx () in this way ()
The following describes some common methods:
1. public static boolean isEmpty (String str)
Determines whether a string is null. If it is null, the standard is str = null or str. length () = 0.
The following is an example of StringUtils determining whether it is null:

StringUtils. isEmpty (null) = true
StringUtils. isEmpty ("") = true
StringUtils. isEmpty ("") = false // note that non-empty spaces are processed in StringUtils.
StringUtils. isEmpty ("") = false
StringUtils. isEmpty ("bob") = false
StringUtils. isEmpty ("bob") = false

2. public static boolean isNotEmpty (String str)
Determines whether a string is not null. It is equal! 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)
Determines whether a string is null or has a length of 0 or is composed of 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 () // both are blank characters
StringUtils. isBlank ("\ B") = false // "\ B" is the word boundary.
StringUtils. isBlank ("bob") = false
StringUtils. isBlank ("bob") = false
4. public static boolean isNotBlank (String str)
Determines whether a string is not empty and its length is not 0. It cannot be composed of whitespace. It is equal! 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)
Removes the control character (char <= 32) at both ends of the string. If the input is null, null is returned.
The following 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 dd") = "d dd"
StringUtils. trim ("dd") = "dd"
StringUtils. trim ("dd") = "dd"
6. public static String trimToNull (String str)
Removes the control character (char <= 32) at both ends of the string. If it is null or "", null is returned.
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 dd") = "d dd"
StringUtils. trimToNull ("dd") = "dd"
StringUtils. trimToNull ("dd") = "dd"
7. public static String trimToEmpty (String str)
Removes the control character (char <= 32) at both ends of the string. If it is null or "", "" is returned ""
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 dd") = "d dd"
StringUtils. trimToEmpty ("dd") = "dd"
StringUtils. trimToEmpty ("dd") = "dd"
8. public static String strip (String str)
Removes the whitespace at both ends of the string. If the input is null, null is returned.
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 dd") = "d dd"
StringUtils. strip ("dd") = "dd"
StringUtils. strip ("dd") = "dd"
9. public static String stripToNull (String str)
Removes the whitespace at both ends of the string. If it is null or "", null is returned.
The following is an example (note the difference between trimToNull () and 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 dd") = "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 is null or "", return ""
The following is an example (note the difference between trimToEmpty () and 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 dd") = "d dd"
StringUtils. stripToNull ("dd") = "dd"
StringUtils. stripToNull ("dd") = "dd"

The following method only describes its functions:
11. public static String strip (String str, String stripChars)
Remove the characters at both ends of str in stripChars.
If str is null or equal to "", return it;
If stripChars is null or "", strip (String str) is returned ).
12. public static String stripStart (String str, String stripChars)
Similar to '11', remove the character in stripChars at the front end of 'str.
13. public static String stripEnd (String str, String stripChars)
Similar to 11, remove the character at the end of str in stripChars.
14. public static String [] stripAll (String [] strs)
Strip (String str) each String in the String array, and then return.
If strs is null or the strs length is 0, the strs itself is returned.
15. public static String [] stripAll (String [] strs, String stripChars)
Strip (String str, String stripChars) each String in the String array, and then return.
If strs is null or the strs length is 0, the strs itself is returned.
16. public static boolean equals (String str1, String str2)
Checks whether two strings are equal. If both strings are null, they are considered equal.
17. public static boolean inclusignorecase (String str1, String str2)
Compares the two strings to determine whether they are equal and case insensitive. If both strings are null, they are considered equal.
18. public static int indexOf (String str, char searchChar)
Returns the position where the searchChar character first appears in the str string.
If searchChar does not appear in str,-1 is returned,
If str is null or "",-1 is returned.
19. public static int indexOf (String str, char searchChar, int startPos)
Returns the position of the first occurrence of the searchChar character in str from startPos.
If searchChar does not appear in str from startPos,-1 is returned,
If str is null or "",-1 is returned.
20. public static int indexOf (String str, String searchStr)
Returns the position where the string searchStr first appears in the string str.
-1 is returned if str is null or searchStr is null,
If searchStr is "" And str is not null, 0 is returned,
If searchStr is not in str,-1 is returned.
21. public static int ordinalIndexOf (String str, String searchStr, int ordinal)
Returns the position where the searchStr string appears at the ordinal Time in the str string.
If str = null or searchStr = null or ordinal <= 0,-1 is returned.
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", "AB", 1) = 1
StringUtils. ordinalIndexOf ("aabaabaa", "AB", 2) = 4
StringUtils. ordinalIndexOf ("aabaabaa", "bc", 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 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)
The basic principle is the same as 18
24. public static int lastIndexOf (String str, char searchChar, int startPos)
Basic Principles 19
25. public static int lastIndexOf (String str, String searchStr)
Basic principles: 20
26. public static int lastIndexOf (String str, String searchStr, int startPos)
The basic principle is the same as 22.
Appendix:
Use of the String split (String regex) Method
If we need to split a String into a String array, we usually use split (String regex.
For example:
Java code replication code

1. String str = "aa, bb, cc, dd ";
2. String [] strArray = str. split (",");
3. System. out. println (strArray. length );
4. for (int I = 0; I <strArray. length; I ++ ){
5. System. out. println (strArray [I]);
6 .}

String str = "aa, bb, cc, dd"; String [] strArray = str. split (","); System. out. println (strArray. length); for (int I = 0; I <strArray. length; I ++) {System. out. println (strArray [I]);}

 

Result:
4
Aa
Bb
Cc
Dd
If,
String str = "aa. bb. cc. dd ";
String [] strArray = str. split (".");
The result is: 0.
Why is the result not what we think? The reason is that the String regex parameter is a regular expression (regular expression) instead of a normal String, and ". "The regular expression has a special meaning, indicating that all individual characters are matched. If you want to split it like that, we must give ". "Escape, String [] strArray = str. split (". ") to String [] strArray = str. split ("\\..
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.