Link to home
Start Free TrialLog in
Avatar of mdlittle
mdlittle

asked on

Delphi Parse a csv

I need to parse a csv file into columns. A typical line will look like this:

"field 1","field 2", "this is more text, that may contain a comma","field4"


Thanks a bunch
ASKER CERTIFIED SOLUTION
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mdlittle
mdlittle

ASKER

Your code works great but I should have been more specific. I really need each column to be parsed or separated. So I think the "pieces" of each line in my file  can be processed or stored individually. I hope I am making sense.

The file has a bunch of these:

"field 1","field 2", "this is more text, that may contain a comma","field4"

I need to take each column in each line and store them somewhere but I don't expect you or anyone else to solve that piece for me. I just need to parse each line in the file and have each "piece" of the line accessible.

Thanks again for the help.
Result := Result + Tmp + #9;

At that line, Tmp is the parsed object, do what you need there.
Thanks a bunch.
procedure TForm1.ParseThis(MyStr: String);
var MS:TStringList;
    i:Integer;
begin
  ListBox1.Clear;
  MS := TStringList.Create;
  MS.CommaText:=MyStr;
  for i:=0 to ms.Count-1 do
    ListBox1.Items.Add(MS.Strings[i]);
  MS.Free;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  ParseThis(Edit1.Text);
end;