I\'m reading from a .csv File line by line. One line could look for example as following: String str = \"10,1,,,,\"
.
Now I would like to split according to
You need to use it with -1
parameter
String[] splitted = str.split(",", -1);
This has been discussed before, e.g. Java: String split(): I want it to include the empty strings at the end
But split
really shouldn't be the way you parse a csv, you could run into problems when you have a String value containing a comma
23,"test,test","123.88"
split
would split the row into 4 parts:
[23, "test, test", "123.88"]
and I don't think you want that.
Pass -1
(or any negative number, actually) as a second parameter to split:
System.out.println("0,,,,,".split(",", -1).length); // Prints 6.
split only drops trailing delimeters by default. You can turn this off with
String str = "9,,,1,,";
String[] parts = str.split(",", -1);
System.out.println(Arrays.toString(parts));
prints
[9, , , 1, , ]