Some confusions about the split (string RegEx) method of the string class in Java
This class is described in the JDK Chinese API as follows:
Public String []Split(String RegEx)
-
-
Splits the string based on the matching of the given regular expression.
The function of this method is like using the given expression and the limit parameter 0 to call two parameters.SplitMethod. Therefore, the result array does not contain trailing null strings.
For example"Boo: And: foo"Produce results with the following expressions:
|
RegEx |
Result |
|
: |
{"Boo", "and", "foo "} |
|
O |
{"B", "", ": And: F "} |
-
-
-
-
-
Parameters:
-
RegEx-Bounded Regular Expression
-
Return Value:
-
String Array, which is split by matching the given regular expression to generate this array.
-
Throw:
-
Patternsyntaxexception-If the regular expression syntax is invalid
-
Start with the following versions:
-
1.4
-
For more information, see:
-
Pattern
CodeAs follows:
String longstring = "AA: BB: CC ";
String [] separator string = longstring. Split (":");
For (INT I = 0; I <struct string. length; I ++ ){
System. Out. println (Bytes string [I]);
}
Obtained: AA, BB, and CC.
It can be proved that there is no problem, but if longstring = "AA | BB | cc", there is a problem. The Code is as follows:
String longstring = "AA | BB | cc ";
String [] separator string = longstring. Split ("| ");
For (INT I = 0; I <struct string. length; I ++ ){
System. Out. println (Bytes string [I]);
}
Then we get a, a, |, B, B, |, C, and C. The problem arises. I don't know why.