Splitting a Java String by the pipe symbol using split(“|”)

前端 未结 7 878
生来不讨喜
生来不讨喜 2020-11-21 23:32

The Java official documentation states:

The string \"boo:and:foo\", for example, yields the following results with these expressions Regex Result :

7条回答
  •  醉话见心
    2020-11-22 00:27

    Use proper escaping: string.split("\\|")

    Or, in Java 5+, use the helper Pattern.quote() which has been created for exactly this purpose:

    string.split(Pattern.quote("|"))
    

    which works with arbitrary input strings. Very useful when you need to quote / escape user input.

提交回复
热议问题