The question is pretty simple.
A CSV file looks like this:
1, "John", "John Joy"
If I want to get each column, I just use String[] splits = line.split(",");
What if the CSV file looks like this:
1, "John", "Joy, John"
So we have a comma inside a double quotes pair. The above split won’t work any more, because I want “Joy, John” as a complete part.
So is there a elegant / simple algorithm to deal with this situation?
Edit:
Please do not consider it as a formal CSV parsing thing. I just use CSV as a use case where I need to split.
What I really want is NOT a proper CSV parser, instead, I just want an algorithm which can properly split a line by comma considering the double quotes.
It’s better to use existing library for this purpuse instead of writing custom implementation (If you don’t do this for studing).
Because CSV has some specifics that you can miss in custom implementation and usually library is well tested.
Here you can find some good one Can you recommend a Java library for reading (and possibly writing) CSV files?
EDIT
I’ve created method that will parse your string but again it could work not perfect because I haven’t tested it well.
It could be just as a start point for you and you can improve it further.
Question for you
What if you will get string like that
1, "John", ""Joy, John""(two quotes on “Joy, John”)?