Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import CSV File using String.split(",") without having the array shortened due to blank fields?

Currently, when I import the CSV file line by line with String.split(",") if there is a line that looks like "foo,,bar,,fizz,,," the method returns an array of length 5, which looks like ["foo", "", "bar, "", "fizz"]. How do I get the method to return an array of length 8 being ["foo", "", "bar", "", "fizz, "", "", ""]?

like image 919
Atocil Avatar asked Nov 19 '12 19:11

Atocil


People also ask

What is import CSV?

Description. The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values. Import-Csv works on any CSV file, including files that are generated by the Export-Csv cmdlet.


1 Answers

There is another overload of split that achieves that:

string.split(",", -1);
like image 113
Marko Topolnik Avatar answered Sep 22 '22 21:09

Marko Topolnik