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 |.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>