There is a String.split() method in the java.lang package, and the return is an array
I have used some of them in the application, and I will summarize them for your reference only:
1. If you use "." as the separation, it must be written as follows, String.split("//."), so that you can separate correctly. String.split(".");
2. If you use "|" as the separation, it must be written as follows, String.split("//|"), so that you can separate correctly. String.split("|");
"." and "|" are both escape characters and must be added with "//";
3. If there are multiple separators in a string, you can use "|" as a hyphen, for example, "acount=? and uu =? or n=?" to separate all three, and you can use String.split("and|or");
When using the String.split method to delimit strings, if the separator uses some special characters, it may not get the results we expected.
Let's look at the description in jdk doc
public String[] split(String regex)
Splits this string around matches of the given regular expression.
The parameter regex is a regular-expression matching pattern instead of a simple String. It may produce unexpected results for some special characters, such as using vertical lines | to separate strings in the following code, you will not get the expected results.
String[] aa = "aaa|bbb|ccc".split("|");//String[] aa = "aaa|bbb|ccc".split("//|"); In order to get the correct result for (int i = 0 ; i <aa.length ; i++ ) {System.out.println("--"+aa[i]); }Running a string with a vertical * will throw a java.util.regex.PatternSyntaxException exception, as will the plus sign +.
String[] aa = "aaa*bbb*ccc".split("*");//String[] aa = "aaa|bbb|ccc".split("//*"); In order to get the correct result for (int i = 0 ; i <aa.length ; i++ ) {System.out.println("--"+aa[i]); }Obviously, + * is not a valid pattern matching rule expression. You can get the correct result after escape with "//*" "//+".
Although "|" is possible when separating strings, it is not the intended purpose. After escape of "//|", the correct result can be obtained.
Also, if you want to use the "/" character in the string, you also need to escape. First, you need to express "aaaa/bbbbb" string. If you want to separate it, you should get the correct result.
String[] aa = "aaa//bbb//bccc".split(////);
The above is the complete description of the usage summary of Java String.split() introduced to you by the editor. I hope it will be helpful to you. If you want to know more, please pay attention to Wulin.com!