字符串分割方法

 

    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]);
    }

  

你可能感兴趣的:(字符串)