Possible Duplicate:
CSV parsing in Java – working example..?
I have a list of names, age and country in the format of "Name",16,"Canada" and some are "First, Second",21,"Canada" how can I separate these?
I have been using .split but cannot get it to work for these format of string.
There are likely libraries that can do this for you (see previous answers).
However, if you want to code it by hand, you will need to build yourself a finite state machine, and examine each character in the string independently to determine whether or not you fall within quotes. You essentially need two states – IN_QUOTE, NO_QUOTE – since the examination rules differ based on your state. If you are within quotation marks, you want to ignore commas. If you are outside quotation marks, then you want commas to separate your fields.
Psuedo code off top of my head would look something like:
All this being said, your examination rules can become overly tricky and complex (do you need to examine for escaped quote marks? What about UTF-8 or other charsets? etc..) and probably not worth your effort to re-invent the wheel when several other libs appear to do this work for you already.