我有一个逗号分隔的文件,其中包含许多类似于下面的行。
Sachin,,M,"Maths,Science,English",Need to improve in these subjects.
引号用于转义用于表示多个值的分隔符逗号。
如果可能的话,现在如何使用 String.split()
在逗号分隔符上拆分上述值?
最佳答案
public static void main(String[] args) {
String s = "Sachin,,M,\"Maths,Science,English\",Need to improve in these subjects.";
String[] splitted = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
System.out.println(Arrays.toString(splitted));
}
输出:
[Sachin, , M, "Maths,Science,English", Need to improve in these subjects.]
关于java - 使用 String.split() 将带有引号的 csv 文件拆分为文本分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738918/