There is a String.Split () method in the Java.lang package, which returns an array
I use in the application of some, to summarize for you, for your reference only:
1. If you use "." As a separate word, it must be written as follows, String.Split ("\ \"), so that the correct separation can not be used String.Split (".");
2. If you use "|" As a separate word, must be the following wording, String.Split ("\\|"), so that the correct separation, can not be used String.Split ("|");
“.” and "|" is an escape character and must be added "\ \";
3. If you have more than one delimiter in a string, you can use the ' | ' As a hyphen, for example, "Acount=?" and UU =? Or n=? ", the three are separated out, you can use String.Split (" And|or ");
When you use the String.Split method to separate strings, separators may not get the results we expect if they use some special characters.
We look at the JDK Doc description
Public string[] Split (String regex)
Splits this string around matches of the given regular expression.
Parameter regex is a regular-expression matching pattern instead of a simple string, and he may have unexpected results for some special characters, such as testing the following code with a vertical bar | Delimited string, you will not get the expected result
string[] aa = "AAA|BBB|CCC". Split ("|") ); // string[] aa = "AAA|BBB|CCC". Split ("\\|") to get the right results for (int i = 0; I <aa.length; i++ ) { System.out.println ("--" +aa[i]);
Running with a vertical * delimited string will throw a Java.util.regex.PatternSyntaxException exception, with a Plus +.
string[] aa = "AAA*BBB*CCC". Split ("*"); // string[] aa = "AAA|BBB|CCC". Split ("\\*") to get the right results for (int i = 0; I <aa.length; i++ ) { System.out.println ("--" +aa[i]); }
Obviously, the + * is not a valid pattern-matching rule expression, and the correct result can be obtained after escaping with "\\*" "\\+".
The "|" string is executed, but not intended, "\\|" The correct results can be obtained after escaping.
Also, if you want to use the "\" character in a string, you also need to escape. First to express "AAAA\BBBB" This string should be used "AAAA\\BBBB", if you want to separate it should be so to get the correct results,
string[] aa = "AAA\\BBB\\BCCC". Split ("\\\\");
Copyright notice: I feel like I'm doing a good job. I hope you can move your mouse and keyboard for me to order a praise or give me a comment, under the Grateful!_____________________________________________________ __ Welcome reprint, in the hope that you reprint at the same time, add the original address, thank you with
How to use the Split function in Java