Reputation: 373
In one of my projects I had to deal with Comma Separated files (CSV). I had to split data based on Comma ,
ignoring commas inside quotes (i.e. ""
) so I used an expression mentioned on another stack overflow question (Java: splitting a comma-separated string but ignoring commas in quotes). Everything was working fine until recently I noticed that it is not working for one specific scenario mentioned below.
I have a data string needed to split on Commas as:
20Y-62-27412,20Y6227412NK,BRACKET,101H,00D505060,H664374,06/25/2013,1,,
In my understanding based on expression
String[] rowData = str.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
Data after splitting should return me an array of size 10 with last two indexes of array containing an empty string, Instead I am getting an array of size 8 being last two commas not treated as splitter. I have used this expression on several places in my application so I don't want to backtrack from this. Any help would be appreciated. Thanks
Upvotes: 2
Views: 4680
Reputation: 4191
You need to use the split(java.lang.String, int) method
Your code would then look like:
String str = "20Y-62-27412,20Y6227412NK,BRACKET,101H,00D505060,H664374,06/25/2013,1,,";
String[] rowData = str.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);
Upvotes: 6