我有一个逗号分隔的文件,其中包含许多类似于下面的行。
Sachin,,M,"Maths,Science,English",Need to improve in these subjects.
引号用于转义用于表示多个值的分隔符逗号。
现在,如果可能的话,如何使用
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.]
由于您的问题/要求并不那么复杂,可以使用自定义方法,其执行速度快 20 倍以上并产生相同的结果。 这是根据数据大小和解析的行数而变化的,对于更复杂的问题,必须使用正则表达式。
import java.util.Arrays;
import java.util.ArrayList;
public class SplitTest {
public static void main(String[] args) {
String s = "Sachin,,M,\"Maths,Science,English\",Need to improve in these subjects.";
String[] splitted = null;
//Measure Regular Expression
long startTime = System.nanoTime();
for(int i=0; i<10; i++)
splitted = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
long endTime = System.nanoTime();
System.out.println("Took: " + (endTime-startTime));
System.out.println(Arrays.toString(splitted));
System.out.println("");
ArrayList<String> sw = null;
//Measure Custom Method
startTime = System.nanoTime();
for(int i=0; i<10; i++)
sw = customSplitSpecific(s);
endTime = System.nanoTime();
System.out.println("Took: " + (endTime-startTime));
System.out.println(sw);
}
public static ArrayList<String> customSplitSpecific(String s)
{
ArrayList<String> words = new ArrayList<String>();
boolean notInsideComma = true;
int start =0, end=0;
for(int i=0; i<s.length()-1; i++)
{
if(s.charAt(i)==',' && notInsideComma)
{
words.add(s.substring(start,i));
start = i+1;
}
else if(s.charAt(i)=='"')
notInsideComma=!notInsideComma;
}
words.add(s.substring(start));
return words;
}
}
在我自己的计算机上会产生:
Took: 6651100
[Sachin, , M, "Maths,Science,English", Need to improve in these subjects.]
Took: 224179
[Sachin, , M, "Maths,Science,English", Need to improve in these subjects.]
如果您的字符串格式正确,则可以使用以下正则表达式:
String[] res = str.split(",(?=([^\"]|\"[^\"]*\")*$)");
该表达式确保分割仅发生在逗号处,逗号后跟偶数(或零)个引号(因此不在此类引号内)。
尽管如此,使用简单的非正则表达式解析器可能会更容易。
最喜欢的答案似乎是正确的,除了尾随逗号的情况。您应该在分割上指定限制参数
public static void main(String[] args) {
String s = "Sachin,,M,\"Maths,Science,English\",Need to improve in these subjects.,,,";
String[] splitted = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);
System.out.println(Arrays.toString(splitted));
}
否则结果将不包括最后三列。