字串分割方法

rilley發表於2014-06-16

 

    public static String[] split(String str, String limit)
    {
        List<String> result = new ArrayList<String>();

        char[] chars = limit.toCharArray();
        int length = str.length();
        int slice = chars.length - 1;

        int match = 0, start = 0, count = 1;

        for (int i = 0; i < length; i++) {
            char c = str.charAt(i);
            if (c == chars[match]) {
                match++;
                if (match == chars.length) {
                    result.add(str.substring(start, i - slice));
                    start = i + 1;
                    count++;
                    match = 0;
                }
            }
            else {
                if (match != 0) match = (c == chars[0]) ? 1 : 0;
            }
        }

        result.add(str.substring(start, length));
        return result.toArray(new String[count]);
    }

  

相關文章