Splitting a csv file with quotes as text-delimiter using String.split()
csv, java, split
Solution
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.]
Problem
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. Now how do I split the above value on the comma delimiter using `String.split()` if at all its possible?