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

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

    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());
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 05:02
    String[] strs = str.split(Pattern.quote("r?"));
    
    0 讨论(0)
提交回复
热议问题