日期:2014-05-20  浏览次数:20736 次

求一个JAVA分割字符串的程序
有一个字符串:aa,bb,cc,"aa,bb,cc",22,33
应该算是序列吧。因为我是从文本是读取出来的 文本里面的就是这样写的。
我现在要进行分割这个序列出来。想以 , 作为分割的标识的。可是 在引号中也会有 , 的。
那如何来进行分割呢?我是用JAVA来操作的
不盛感激啊。

------解决方案--------------------
Java code
public static void main(String[] args) throws ParseException {
        String str = "aa,bb,cc,\"aa,bb,cc\",22,33 ";
        String[] result = split(str, ',');
        for (String string : result) {
            System.out.println(string);
        }
    }

    public static String[] split(String source, char ch) {
        List<String> values = new ArrayList<String>();
        StringBuffer temp = new StringBuffer();
        int count = 0;
        for (int i = 0; i < source.length(); i++) {
            char currentChar = source.charAt(i);
            if (currentChar == ch) {
                if (count == 0) {
                    if (i == 0) {
                        continue;
                    }
                    values.add(temp.toString());
                    temp = new StringBuffer();
                } else {
                    temp.append(currentChar);
                }

            } else if (currentChar == '"') {
                if (count == 0) {
                    count++;
                } else {
                    count--;
                }
                temp.append(currentChar);
            } else {
                temp.append(currentChar);
            }
        }
        // if the last string is not blank, trim it and add this value to result
        if (temp.toString().trim().length() != 0) {
            values.add(temp.toString().trim());
        }
        String[] result = new String[values.size()];
        for (int i = 0; i < values.size(); i++) {
            result[i] = values.get(i);
        }
        return result;
    }

------解决方案--------------------
改了一下,改正了存在奇数个"分割不正确的问题
Java code
public static String[] split(String source, char ch) {
        List<String> values = new ArrayList<String>();
        StringBuffer temp = new StringBuffer();
        int count = 0;
        for (int i = 0; i < source.length(); i++) {
            char currentChar = source.charAt(i);
            if (currentChar == ch) {
                if (count == 0) {
                    if (i == 0) {
                        continue;
                    }
                    values.add(temp.toString());
                    temp = new StringBuffer();
                } else {
                    temp.append(currentChar);
                }

            } else if (currentChar == '"') {
                if (count == 0) {
                    count++;
                } else {
                    count--;
                }
                temp.append(currentChar);
            } else {
                temp.append(currentChar);
            }
        }

        // if the last string is not blank, trim it and add this value to result
        if (temp.toString().trim().length() != 0) {
            if (count != 0) {
                String[] temps = temp.toString().trim().split(",");
                for (String string : temps) {
                    values.add(string);
                }
            } else {
                values.add(temp.toString().trim());
            }

        }
        String[] result = new String[values.size()];
        for (int i = 0; i < values.size(); i++) {
            result[i] = values.get(i);
        }
        return result;
    }