For example, execute: "2|33|4".split("|")
The result is:
""
2
3
3
4
It's strange, but if you read the API description carefully, you can still understand the reason.
java.lang.string.split
split method
Splits a string into substrings and returns the result as an array of strings.
stringObj.split([separator, [limit]])
parameter
stringObj
Required. The String object or literal to be decomposed. This object will not be modified by the split method.
separator
Optional. A string or regular expression object that identifies whether one or more characters are used to separate the string. If this option is omitted, a single-element array containing the entire string is returned.
limit
Optional. This value is used to limit the number of elements in the returned array.
illustrate
The result of the split method is a string array, which must be decomposed at every position where separator appears in stingObj.
So the normal way to write it is like this:
1. If "." is used as a separation, it must be written as follows: String.split("//."), so that it can be separated correctly. String.split(".") cannot be used;
2. If "|" is used as a separator, it must be written as follows: String.split("//|"), so that it can be separated correctly. String.split("|") cannot be used;
"." and "|" are both escape characters, and "//" must be added;
3. If there are multiple delimiters in a string, you can use "|" as a hyphen, such as: "a=1 and b =2 or c=3". To separate all three, you can use String. split("and|or");