How can I read and write CSV in a way similar to NET FileHelpers?

后端 未结 7 750
感动是毒
感动是毒 2021-02-03 12:11

Anyone know how I can import/export csv, txt files in a way similar to NET FileHelpers, but using Delphi, taking spaces and quotes into account and handling traditional CSV esca

7条回答
  •  既然无缘
    2021-02-03 12:46

    My functions

    function ParseCSVString(s: string; const delimiter: Char = ','; const enclosure: Char = '"'): TStrings;
    var
        i,len: Integer;
        f: string;
        inQuoted: Boolean;
    begin
        Result := TStringList.Create;
        len := Length(s);
        if len = 0 then Exit;
        //Test,Test;"Test;Test";"Test""Test";;
        f := '';
        inQuoted := False;
        i:=0;
        while i < len do
        begin
            Inc(i);
            if s[i] = enclosure then
            begin
                if inQuoted and (i 0) OR (Pos(enclosure,s) > 0) then
            Result := enclosure+Result+enclosure;
    end;
    

提交回复
热议问题