Since String.split()
works with regular expressions, this snippet:
String s = "str?str?argh";
s.split("r?");
... yields: [, s, t, , ?, s, t, , ?, a, , g, h]
What's the most elegant way to split this String on the r?
sequence so that it produces [st, st, argh]
?
EDIT: I know that I can escape the problematic ?
. The trouble is I don't know the delimiter offhand and I don't feel like working this around by writing an escapeGenericRegex()
function.
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.
Commenters have noted that there are scenarios in which using Pattern.compile
and caching the resulting Matcher
has performance benefits. But the counter to that is that there are scenarios where it is not beneficial (or the benefit is insignificant). And in a multi-threaded application, sharing a Matcher
across multiple threads is potentially problematic because Matcher
is not thread-safe. The bottom line is that discussion of using Pattern.compile
(or not) is a red herring in the context of the question asked.