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

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.


You can use

StringUtils.split("?r")

from commons-lang.


This works perfect as well:

public static List<String> splitNonRegex(String input, String delim)
{
    List<String> l = new ArrayList<String>();
    int offset = 0;

    while (true)
    {
        int index = input.indexOf(delim, offset);
        if (index == -1)
        {
            l.add(input.substring(offset));
            return l;
        } else
        {
            l.add(input.substring(offset, index));
            offset = (index + delim.length());
        }
    }
}

Escape the ?:

s.split("r\\?");

String[] strs = str.split(Pattern.quote("r?"));