Considerations for using split in Java
Java uses split application scenarios, usually for string segmentation. For example, the front end has such a parameter, such as {"year": "2018 | 2019 | 2020"}
Our backend reception needs further processing. If we need to separate 2018 | 2019 | 2020 one by one, we need to use the split method.
I encountered such a problem when using split to split strings.
Error code:
In fact, the following results cannot be split and output:
Correct code:
So the question is, why do you need to escape?
View the corresponding source code as follows:
The answer is that the underlying matches are based on regular expressions. If the escape character is not used, the escape provided by Java may be recognized as dividing 2019 | 2018 | 2020 one by one, and the last one must be a single one, such as 2, 0, 1, 9 and |.