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

前端 未结 8 918
没有蜡笔的小新
没有蜡笔的小新 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:54

    A general solution using just Java SE APIs is:

    String separator = ...
    s.split(Pattern.quote(separator));
    

    The quote method returns a regex that will match the argument string as a literal.

提交回复
热议问题