The Java. lang package contains the string. Split () method, and an array is returned.
1. If ". "As the separator, it 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 escape characters and must be added "//";
3. If a string contains multiple separators, you can use "|" as a hyphen, for example, "acount =? And UU =? Or n =? ", All three are separated. You can use string. Split (" and | or ");
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.
The RegEx parameter is a regular-expression (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 ("// | ");
For (INT I = 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 (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 ("////");