String. In Java Summary of split() usage
In Java Lang package contains string The split () method returns an array
I use some in the application to summarize for your reference only:
1. If you use "." As a separator, it must be written as follows, string split("\\."), In this way, you can separate them correctly. You can't use string split(".");
2. If you use "|" as a separator, string Split ("\ \ |"), cannot use string split("|");
“.” And "|" are escape characters, and "\ \" must be added;
3. If there are multiple separators in a string, you can use "|" as a hyphen, for example, "account =? And UU =? Or n =?", To separate all three, you can use string split("and|or");
Use string When the split method separates strings, if some special characters are used in the separator, we may not get the expected results.
Let's see the description in JDK doc
Splits this string around matches of the given regular expression.
The parameter regex is a regular expression matching pattern rather than a simple string. It may produce unexpected results for some special characters. For example, if you test the following code to separate strings with vertical bars, you will not get the expected results
Running a string separated by a vertical * will throw Java util. regex. Patternsyntaxexception exception, the same is true with plus sign +.
Obviously, + * is not a valid pattern matching rule expression. You can get the correct result after escaping with "\ \ *" and "\ \ +".
Although "|" can be executed when separating strings, it is not the intended purpose. The correct result can be obtained after "\ \ |" is escaped.
Also, if you want to use the "\" character in the string, you also need to escape First, to express "AAAA \ BBBB", the string should be "AAAA \ \ BBBB". If it is to be separated, it should be so as to get the correct result,
The above is the Java string introduced by Xiaobian Split () Usage Summary of all the statements, I hope to help you, if you want to know more, please pay attention to the programming tips!