String Tokenizer:用逗号分隔字符串,用双引号忽略逗号 [英] String Tokenizer : split string by comma and ignore comma in double quotes
问题描述
我有一个如下字符串 -
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屋!