Reputation: 77
I am parsing csv file, and I have this row..
45,12,bruine verbinding,mechelse heide,bruin,"276,201,836,338,468",01050000208A7A000001000000010200000002000000443BFF11CF720D41F296072200BD0641189D9C0D026D0D417A50F15264C30641,"MULTILINESTRING((241241.883787597 186272.016616039,241056.256646373 186476.540499333))"
When I convert this string into array by the method
NSArray *arrObjects=[strObjects componentsSeparatedByString:@","];
Then I get 13 objects rather I want 8 objects of array. Objects at index 5 further splits up into five more objects instead of one object(because this object has further commas (,) in string) and index 7 further splits up into 2 objects.
I want only full string object of at index 5 and index 7 instead of five and two objects respectively. I know this is because of method componentsSeparatedByString:@","
.
Upvotes: 0
Views: 1192
Reputation: 131481
Since the CSV standard allows commas to appear inside a record you can't blindly use componentsSeparatedByString:@","
to separate the fields.
It is actually a rather fussy problem to write a CSV parser that can handle line breaks, commas, and quotation marks as field data.
I would suggest either:
Dictating that the data for a field NOT contain commas, line breaks, or quotes (percent escape each field before saving it to the CSV)
or, if you must deal with data in that format, use an existing CSV library.
A quick Google search on "objective-c csv parser" shows this on Github:
Since it claims to be a "proper CSV parser" it should handle fields containing commas.
Upvotes: 1