String.split() *not* on regular expression?

前端 未结 8 922
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 04:31

Since String.split() works with regular expressions, this snippet:

String s = \"str?str?argh\";
s.split(\"r?\");

... yields: <

8条回答
  •  借酒劲吻你
    2020-12-03 04:50

    Using directly the Pattern class, is possible to define the expression as LITERAL, and in that case, the expression will be evaluated as is (not regex expression).

    Pattern.compile(, Pattern.LITERAL).split();
    

    example:

    String[] result = Pattern.compile("r?", Pattern.LITERAL).split("str?str?argh");
    

    will result:

    [st, st, argh]
    

提交回复
热议问题