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.
Public String [] Split (string RegEx)
Splits this string around matches of the given regularexpression.
The RegEx parameter is a regular-expression matching mode instead of a simple string. It may produce unexpected results for some special characters, for example, test the following code:
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 ("\\|"); in this way, the correct result can be obtained.
For (INTI = 0; I <AA. length; I ++ ){
System. Out. println ("--" + AA [I]);
}
Running a string separated by vertical bars will throw a java. util. RegEx. patternsyntaxexception, as is the case with the plus sign +.
String [] AA = "AAA * BBB * CCC". Split ("*");
// String [] AA = "AAA | BBB | CCC". Split ("\ *");
For (INTI = 0; I <AA. length; I ++ ){
System. Out. println ("--" + AA [I]);
}
Obviously, + * is not a valid Regular Expression for pattern matching. You can use "*" "+" to escape the expression and get the correct result.
"|" 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. first, we need to express the "aaaabbbb" string and use "aaaabbbb". If we want to separate it, we can get the correct result:
String [] AA = "aaabbbbccc". Split ("");