String Tokenizer : split string by comma and ignore comma in double quotes

后端 未结 6 2052
闹比i
闹比i 2021-01-18 03:58

I have a string like below -

value1, value2, value3, value4, \"value5, 1234\", value6, value7, \"value8\", value9, \"value10, 123.23\"

6条回答
  •  悲&欢浪女
    2021-01-18 04:30

    I'm allergic to regex; why not double-split as someone suggested?

        String str = "value1, value2, value3, value4, \"value5, 1234\", value6, value7, \"value8\", value9, \"value10, 123.23\"";
        boolean quoted = false;
        for(String q : str.split("\"")) {
            if(quoted)
                System.out.println(q.trim());
            else
                for(String s : q.split(","))
                    if(!s.trim().isEmpty())
                        System.out.println(s.trim());
            quoted = !quoted;
        }
    

提交回复
热议问题