我有一个用逗号分隔的文件,其中有很多行类似于下面的内容。
Sachin,,M,"Maths,Science,English",Need to improve in these subjects.
引号用于转义表示多个值的定界符逗号。
现在,如何String.split()在可能的情况下使用逗号分隔上面的值?
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.]