I believe that everyone often uses the Split method of String, but have you encountered the following situations:
Everyone think about what is the following code execution result
Public Static Void Main (String [] args) {// Todo Auto-Generatd Method Stub String Str1 = "A, B, C, A"; String Str2 = "A, B, C ,,"; String Str3 = "a, b, c,,," "; string [] s1 = str1.split (", ","); string [] s2 = str2.split (","); string [] s3 = str3.split ("" "" "" , "); System.out.println (" STR1 length: "+s1.Length); System.out.println (" STR2 length: "+s2.Length); system.out.println (" STR3 length: "+"+ s3.length);}
Execution results:
Why does this result appear? Find the API to find the solution
Solution:
By viewing the API, we found that our commonly used Split method is transmitted by 0 by default. Now the solution to solving the STR2 output space is the second parameter that is passed to
Public Static Void Main (String [] args) {// Todo Auto-Generatd Method Stub String Str1 = "A, B, C, A"; String Str2 = "A, B, C ,,"; String Str3 = "a, b, c,,," "; string [] s1 = str1.split (", "); string [] s2 = str2.split (", ",",-1); string [] s3 = str3. split (",", -1); System.out.println ("STR1 length:"+s1.length); System.out.println ("STR2 length:"+s2.length); System.out.println ( "STR3 length:"+s3.length);}
After finding the API, I found that in the String class, there are two Split heavy load methods
1. Public String [] Split (String Regex)
This strings are split according to the given regular expression.
The role of this method is like using a given expression and limit parameter 0 to call the two -parameter Split method. Therefore, the array does not include the end -empty string.
For example, the string "Boo: and: FOO" can generate the following results:
Regex results
: {"Boo", "AND", "Foo"} o {"b", "", ": and: f"}
parameter:
Regex -Fixed Law regular expression returns:
The string array, which is thrown out according to the matching of the regular expression expression to split this string:
PatternsyntaxException -If the grammar in the regular expression is invalid
2. Public String [] Split (String Regex, int Limit)
This strings are split according to the regular expression given by matching.
The array returned by this method contains the sub -string of this string. Each sub -string is terminated by another sub -string of a given expression, or the end of this string is terminated. The sub -string in the array is arranged in the order they appear in this string. If the expression does not match any part of the input, the income array has only one element, that is, this string.
Limit parameter control mode applications, thus affecting the length of the array. If this limit n is greater than 0, the mode will be used most N -once, and the length of the array will not be greater than N, and the last item of the array will contain all inputs beyond the final matching boundary. If n is non -positive, the mode will be used as much as possible, and the array may be any length. If n is 0, then the mode will be used as much as possible, the array can be any length, and the end -empty string will be discarded.
For example, the string "Boo: and: FOO" can generate the following results with these parameters:
Regexlimit results
: 2 {"Boo", "AND: FOO"}: 5 {"Boo", "AND", "Foo"}: -2 {"Boo", "AND", "Foo"} o 5 {"b" "", ": and: f", "", "} o -2 {" b "," ",": and: f "," ","} o 0 {"b", " ": and: f"}
The form of str.Split (regex, n) that calls this method is exactly the same as the results produced by the following expressions:
Pattern.Compile (regex) .split (str, n)
parameter:
Regex -Definition regular expression
Limit -result threshold, as described above
return:
The string array, it is determined according to the matching of the regular expression expression.
Throwing out:
PatternsyntaxException -If the grammar in the regular expression is invalid