Splitting a csv file with quotes as text-delimiter using String.split()

前端 未结 3 636
离开以前
离开以前 2020-11-27 11:07

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

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

3条回答
  •  时光说笑
    2020-11-27 11:36

    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));
    }
    

    Output:

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

提交回复
热议问题