In the jdk api documentation, the split method is described in detail, but hidden information cannot be displayed without careful consideration.
An instance that breaks down the string "Boo: And: foo"
Package mark. zhang; <br/> public class testsplit {<br/> Public static void main (string [] ARGs) {<br/> string STR = "Boo: And: foo "; <br/> string [] temp = Str. split ("O",-1); <br/> for (INT I = 0; I <temp. length; I ++) {<br/> system. out. println (temp [I]); <br/>}< br/>}
In this example, calling the split (string RegEx) method is equivalent to calling its overload method split (string RegEx, int limit), limint = 0, print the result:
B <br/>: And: F <br/> // ["B", "", ": And: F"]
Analyze the reason for the result. At first I thought the result was ["B", ": And: F"], but it turned out to be counterproductive. Before analyzing the results, you need to understand the following sentence:
If n is zero then the pattern will be applied as usual times as possible, the array can have any length, and trailing empty strings will be discarded <br/> if n is 0, the pattern will be applied as many times as possible, the array can be of any length, and the trailing null string will be discarded. <Br/> All null strings at the end of the array will be lost <br/> Note: Here N is the int of Split (string, INT ).
"Boo: And: foo"
The first time "B" and "O: And: foo" are obtained using an O separator"
For the second time, separate "" and ": And: foo" with "O"
For the third time, separate ": And: F" and "O" with "O"
For the fourth time, use the "O" separator to obtain "" and "" because "O" are separated, and then the left and right empty strings can be obtained, that is, the left and right empty strings are ignored.
The result is "B": And: F"
The code for modifying the preceding example is as follows:
Package mark. zhang; <br/> public class testsplit {<br/> Public static void main (string [] ARGs) {<br/> string STR = "Boo: And: foo "; <br/> string [] temp = Str. split ("O",-1); <br/> for (INT I = 0; I <temp. length; I ++) {<br/> system. out. println (temp [I]); <br/>}< br/> system. out. println ("-- = --"); <br/>}< br/>}
The running result is as follows:
B <br/>: And: F </P> <p> -- = -- <br/> // ["B", "", ": and: F "," "," "]
Combined with the API, the analysis results are reliable. one sentence:
If n is non-positive then the pattern will be applied as usual times as possible and the array can have any length. <br/> if n is not positive, the mode will be applied as many times as possible, and the array can be of any length.
OK. The two empty strings at the end are not removed this time. Let's look at another instance and change n to positive number 1, as shown below:
Package mark. zhang; <br/> public class testsplit {<br/> Public static void main (string [] ARGs) {<br/> string STR = "Boo: And: foo "; <br/> string [] temp = Str. split ("O", 1); <br/> for (INT I = 0; I <temp. length; I ++) {<br/> system. out. println (temp [I]); <br/>}< br/> system. out. println ("-- = --"); <br/>}< br/>}
The result is "Boo: And: foo", yes, no decomposition, why ???
If the limit N is greater than zero then the pattern will be applied at most n-1 times, the array's length will be no greater than N, and the array's last entry will contain all input beyond the last matched delimiter <br/> If the limit N is greater than 0, the mode will be applied up to n-1 times, the length of the array will not be greater than N, and the last entry of the array will contain all input that exceeds the final match.
Because only n-1 times are executed, 1-1 = 0 times are not separated to obtain the original string. Change to another positive number. The result is displayed on the jdk api:
Regexlimitresult <br/>: 2 {"boo", "And: foo"} <br/>: 5 {"boo", "and ", "foo"} <br/>:-2 {"boo", "and", "foo"} <br/> O5 {"B ","",": and: F "," ","} <br/> O-2 {"B", "", ": And: F ","", ""} <br/> O0 {"B", "", ": And: F "}
Limit = 9, which is the same as limit = 5 in this string.
Repost one article:
Tips for using the split function in Java
Source: http://www.cnblogs.com/liubiqu/archive/2008/08/14/1267867.html
The string. Split () method is also available in the Java. lang package. Similar to. net, the return is a struct-type array, but there are some tips during use. For example:
"2 | 33 | 4". Split ("| ")
The result is:
"" <Br/> 2 <br/> | <br/> 3 <br/> 3 <br/> | <br/> 4
It's strange, but check the API description to find out why.
Java. Lang. String. Split <br/> the split method <br/> Splits a string into substrings and returns the result as a string array. <Br/> stringobj. Split ([separator, [limit]) <br/> parameter <br/> stringobj <br/> required. String object or text to be decomposed. This object will not be modified by the split method. <Br/> separator <br/> optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If this option is ignored, a single array of elements containing the entire string is returned. <Br/> limit <br/> optional. This value is used to limit the number of elements in the returned array. <Br/> description <br/> the result of the split method is a string array. In stingobj, the position of each separator must be decomposed.
Therefore, the normal syntax is as follows:
1. If ". ", must be written as follows: string. split ("//. "), in this way, the correct separation is not allowed using string. split (". ");
2. If "|" is used as the separator, it must be written as follows: string. split ("// |"), which can be correctly separated. String is not allowed. split ("| ");
"." And "|" are both escape characters and must be added "//";
3. If a string contains multiple separators, you can use "|" as a hyphen, for example, "A = 1 and B = 2 or C = 3 ", you can use string to separate all three. split ("and | or ");