I might be wrong here, but as far as I remember, in CSV the first line
tells us what is the seperator of fields, and only if there is no such
line, you need to assume that it is ",".

Regardsing the " issue, you first need to retrive the item, and then
"strip" the " char...

Ido

On 6/6/06, Alexandre Leclerc <[EMAIL PROTECTED]> wrote:
2006/6/5, Luiz Americo Pereira Camara <[EMAIL PROTECTED]>:
> Alexandre Leclerc escreveu:
> > I checked arround to see if there was a function to parse a valid CSV
> > string and return the result in a array?
> >
> > procedure DecodeCSV(s: string; var a: array of string);
> >
> > So a string like:
> > 123, my name, "name, my", "just ""more"" text"
> >
> > would return in an array:
> > [ '123' , 'my name' , 'name, my', 'just "more" text' ]
> >
> Take a look at this:
>
> procedure CSVToStrings(const ACSVStr: String; AStrList: TStrings);
> var
>   pos1,pos2:Integer;
> begin
>   pos1:=1;
>   pos2:=pos(';',ACSVStr);
>   while pos1 < pos2 do
>   begin
>     AStrList.Add(Copy(ACSVStr,pos1,pos2-pos1));
>     pos1:=pos2+1;
>     pos2:=posex(';',ACSVStr,pos1);
>   end;
>   if pos1 < length(Trim(ACSVStr)) then
>     AStrList.Add(Copy(ACSVStr,pos1,succ(length(ACSVStr)-pos1)));
> end;

This does the job. :) I'm finishing my function this morning. I think
I'll simply contribute it to the strutils of fpc when it will be done.
It will be flexible enought to handle many CSV like files, like your
exmaple, using ';' instead of ','. And also usefull to parse a
PostgreSQL screen dump (they use '|' as separator).

--
Alexandre Leclerc



_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to