Regarding Java Split Command Parsing Csv File

后端 未结 2 1152
醉酒成梦
醉酒成梦 2020-12-11 22:42

I have a csv file in the below format.

H,\"TestItems_20100107.csv\",07/01/2010,20:00:00,\"TT1198\",\"MOBb\",\"AMD\",NEW,,

I require the spl

2条回答
  •  孤街浪徒
    2020-12-11 22:59

    I came across this same problem today and found a simpe solution for csv files: adding an extra field containing just one space at the time the split is executed:

    (line + ", ").split(",");
    

    This way no matter how many consecutive empty fields may exist at the end of the csv file, split() will return always n+1 fields

    Example session (using bsh)

    bsh % line = "H,\"TestItems_20100107.csv\",07/01/2010,20:00:00,\"TT1198\",\"MOBb\",\"AMD\",NEW,,
    bsh % System.out.println(line);
    H,"TestItems_20100107.csv",07/01/2010,20:00:00,"TT1198","MOBb","AMD",NEW,,
    bsh % String[] items = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    bsh % System.out.println(items.length);
    8
    bsh % items = (line + ", ").split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    bsh % System.out.println(items.length - 1 );
    10
    bsh %
    

提交回复
热议问题