Split definition and usage
The split () method is used to split a string into a string array.
Syntax
StringObject. split (separator, howator)
Parameter description
Separator is required. String or regular expression, which separates stringObject from the specified place.
Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length.
Return Value
A string array. This array is created by dividing the string stringObject into substrings at the boundary specified by separator. The strings in the returned array do not include the separator itself.
However, if separator is a regular expression that contains a subexpression, the returned array contains the strings that match the subexpression (but does not include the text that matches the entire regular expression ).
The following is an example.
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Advanced split skills (Special string processing ):
The java. lang package contains the String. split () method, and an array is returned.
I have used some of them in the application. I will summarize them for your reference only:
1. If ". ", 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 ("\ |"), in order to correctly separate, cannot use String. split ("| ");
3. If "\" is used as the separator, it must be written as follows: String. split (\), in order to correctly split, cannot use String. split ("\");
".", "|", And "\" are escape characters and must be added "\\";
3. If a string contains multiple delimiters, you can use "|" as a hyphen, for example, "acount =? And uu =? Or n = ?", Use String. split ("and | or") to separate all three ");
Example 1:
If you want to use the "\" character in the string, you also need to escape it. for example, to express the string "aaaa \ bbbb", you should use "aaaa \ bbbb". If you want to separate the strings, you should get the correct result:
String [] aa = "aaa \ bbb \ bccc". split (\\\\);
Example 2: