When the string. split method is used to separate strings, if some special characters are used as separators, the expected results may not be obtained.
Let's seeJDK Doc description
Public String []Split(String RegEx)
Splits this string around matches of the given regular expression.
ParametersRegEx is The regular-expression matching mode is not a simple string. It may produce unexpected results for some special characters, such asCode:
Use vertical bars | to separate strings and you will not get the expected results
String [] AA = "AAA | BBB | CCC". Split ("| ");
// String [] AA = "AAA | BBB | CCC". Split ("// | ");
For (INT I = 0; I <AA. length; I ++ ){
System. Out. println ("--" + AA [I]);
}
running with vertical * Separator will throw Java. util. regEx. the patternsyntaxexception is abnormal, as is the case with the plus sign +.
String [] AA = "AAA * BBB * CCC". Split ("*");
// String [] AA = "AAA | BBB | CCC". Split ("// *");
For (INT I = 0; I <AA. length; I ++ ){
System. Out. println ("--" + AA [I]);
}
Obviously, + * is not a valid Regular Expression for pattern matching. You can get the correct result after escaping it with "// *" "// +.
"|" The separator string can be executed, but it is not the expected purpose. "// |" escape to get the correct result.
If you want to use the "/" character in the string, you also need to escape it. the expression "AAAA/BBBB" should be "AAAA // BBBB" first. If you want to separate the strings, you should get the correct result:
String [] AA = "AAA // BBB // BCCC". Split ("////");
Because I do not have a deep understanding of regular-expression, I also hope that the high finger is positive and supplement.