字符串,分割(“,”)不太可能起作用。
它会分割嵌入逗号(“Foo, Inc.”)的字段,即使它们是 CSV 行中的单个字段。
如果公司名称是:
公司
或者更糟:
乔的“又好又快又便宜”的食物
根据维基百科: (http://en.wikipedia.org/wiki/Comma-separated_values http://en.wikipedia.org/wiki/Comma-separated_values)
嵌入逗号的字段必须用双引号字符括起来。
1997,Ford,E350,"Super, luxurious truck"
具有嵌入双引号字符的字段必须用双引号字符括起来,并且每个嵌入的双引号字符必须由一对双引号字符表示。
1997,Ford,E350,"Super ""luxurious"" truck"
更糟糕的是,引用的字段可能有嵌入的换行符(换行符;“\n”):
具有嵌入换行符的字段必须用双引号字符引起来。
1997,Ford,E350,"Go get one now
they are going fast"
这说明了问题字符串,分割(“,”)解析逗号:
CSV 行是:
a,b,c,“Company, Inc.”, d, e,“Joe 的“又好又快又便宜”的食物”, f, 10/11/2010,1/1/2011, g, h,我
// Test String.split(",") against CSV with
// embedded commas and embedded double-quotes in
// quoted text strings:
//
// Company names are:
// Company, Inc.
// Joe's "Good, Fast, and Cheap" Food
//
// Which should be formatted in a CSV file as:
// "Company, Inc."
// "Joe's ""Good, Fast, and Cheap"" Food"
//
//
public class TestSplit {
public static void TestSplit(String s, String splitchar) {
String[] split_s = s.split(splitchar);
for (String seg : split_s) {
System.out.println(seg);
}
}
public static void main(String[] args) {
String csvLine = "a,b,c,\"Company, Inc.\", d,"
+ " e,\"Joe's \"\"Good, Fast,"
+ " and Cheap\"\" Food\", f,"
+ " 10/11/2010,1/1/2011, h, i";
System.out.println("CSV line is:\n" + csvLine + "\n\n");
TestSplit(csvLine, ",");
}
}
产生以下内容:
D:\projects\TestSplit>javac TestSplit.java
D:\projects\TestSplit>java TestSplit
CSV line is:
a,b,c,"Company, Inc.", d, e,"Joe's ""Good, Fast, and Cheap"" Food", f, 10/11/2010,1/1/2011, g, h, i
a
b
c
"Company
Inc."
d
e
"Joe's ""Good
Fast
and Cheap"" Food"
f
10/11/2010
1/1/2011
g
h
i
D:\projects\TestSplit>
CSV 行应该解析为:
a
b
c
"Company, Inc."
d
e
"Joe's ""Good, Fast, and Cheap"" Food"
f
10/11/2010
1/1/2011
g
h
i