我有一个字符串:A,B,"a,b,c",C
现在需要吧双引号中间的逗号去掉。但是又不确定双引号中间部分有几个逗号,因为这部分是金额。
我需要把这个串分割,双引号之间的逗号会影响分割。
高手给指点下谢谢。

解决方案 »

  1.   


    String str="A,B,\"a,b,c\",C";
    str=str.replace("\",", "\"").replace(",\"", "\"");
    System.out.println(str);
      

  2.   

    public class Test {    public static void main(String args[]) {        
            String str = "\"a,b,c\",A,\"a,b,c\",B,\"a,b,c\",C,\"a,b,c\"";
            String regex = ",(?![^\"]*\"\\s*(?:,|$))";        
            String[] strs = str.split(regex);
            System.out.println(str);
            System.out.println();
            for(String s : strs) {
                System.out.println(s);
            }
        }
    }
      

  3.   

    public static void main(String[] args) {

    String s = "A,B, \"0, b, c\",C , \"4, 5, 6, 7,7, 9\"";


    String[] ss = s.split("\"[0-9A-Za-z,\\s]*\"");
    int offset = 0;
    int from = 0, to = 0;
    StringBuffer sb = new StringBuffer();
    int i = 0;
    for (; i < ss.length; ++i) { offset = s.indexOf(ss[i]);
    from = offset + ss[i].length();
    if (i == ss.length - 1) {
    to = s.length();
    } else {
    to = s.indexOf(ss[i + 1], offset);
    }

    String m = s.substring(from, to);
    m = m.replaceAll(",", "");
    m = m.replaceAll(" ", "");
    sb.append(s.substring(offset, from));
    sb.append(m);

    }
    System.out.println(sb.toString());

    }
      

  4.   

    替换引号中的逗号:str = str.replaceAll(",(?=[^\"]*?\"\\s*(?:,|$))", "");
      

  5.   

    [code=Java]
    public static void main(String[] args) throws IOException {
    String str="A,B,\"aaaaaaaaa,bbb,ccccc\",C";
    System.out.println(str);
    String[] str3 = str.split("(?<!(,\"\\w{0,10})),(?!(\\w{0,10}\",))");
    for(int i=0;i<str3.length;i++){
    System.out.println(str3[i]);
    }
    }
    [code]中间的 \\w{0,10}  ..  想用\*的,不过好象有点问题,
    将就用吧,如果字符串长度大于10 自己手动把10改大点
      

  6.   

    str.split("(? <!(,\"\\w{0,10})),(?!(\\w{0,10}\",))");
    str.split("(?<!(,\"\\w{0,10})),(?!(\\w{0,10}\",))");
    帖子的格式有点问题,中间老多个空格..自己注意吧