String Tokenizer:用逗号分隔字符串,用双引号忽略逗号 [英] String Tokenizer : split string by comma and ignore comma in double quotes

查看:172
本文介绍了String Tokenizer:用逗号分隔字符串,用双引号忽略逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下字符串 -

I have a string like below -


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

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

如果我在字符串上方标记,我会得到逗号分隔的标记。但是我想在进行拆分时用双引号后的字符串标记器忽略逗号。我该怎么说?

If I tokenize above string I'm getting comma separated tokens. But I would like to say to string tokenizer ignore comma's after double quotes while doing splits. How can I say this?

提前致谢

Shashi

推荐答案

使用像 OpenCSV 这样的CSV解析器来处理类似的问题引用元素中的逗号,自动跨越多行等的值。您也可以使用该库将文本序列化为CSV格式。

Use a CSV parser like OpenCSV to take care of things like commas in quoted elements, values that span multiple lines etc. automatically. You can use the library to serialize your text back as CSV as well.

String str = "value1, value2, value3, value4, \"value5, 1234\", " +
        "value6, value7, \"value8\", value9, \"value10, 123.23\"";

CSVReader reader = new CSVReader(new StringReader(str));

String [] tokens;
while ((tokens = reader.readNext()) != null) {
    System.out.println(tokens[0]); // value1
    System.out.println(tokens[4]); // value5, 1234
    System.out.println(tokens[9]); // value10, 123.23
}

这篇关于String Tokenizer:用逗号分隔字符串,用双引号忽略逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆