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

前端 未结 3 630
离开以前
离开以前 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:40

    As your problem/requirements are not all that complex a custom method can be utilized that performs over 20 times faster and produces the same results. This is variable based on the data size and number of rows parsed, and for more complicated problems using regular expressions is a must.

    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 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 customSplitSpecific(String s)
    {
        ArrayList words = new ArrayList();
        boolean notInsideComma = true;
        int start =0, end=0;
        for(int i=0; i

    }

    On my own computer this produces:

    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.]
    

提交回复
热议问题