使用 String.split() 拆分带有引号作为文本分隔符的 csv 文件 [英] Splitting a csv file with quotes as text-delimiter using String.split()

查看:32
本文介绍了使用 String.split() 拆分带有引号作为文本分隔符的 csv 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个逗号分隔的文件,其中有很多行,类似于下面的一行.

I have a comma separated file with many lines similar to one below.

Sachin,,M,"Maths,Science,English",Need to improve in these subjects.

引号用于转义用于表示多个值的分隔符逗号.

Quotes is used to escape the delimiter comma used to represent multiple values.

如果可能的话,现在如何使用 String.split() 在逗号分隔符上拆分上述值?

Now how do I split the above value on the comma delimiter using String.split() if at all its possible?

推荐答案

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.]

这篇关于使用 String.split() 拆分带有引号作为文本分隔符的 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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