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

查看:873
本文介绍了使用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天全站免登陆