Einzelnen Beitrag anzeigen

Benutzerbild von milos
milos

Registriert seit: 14. Jul 2008
Ort: Bern (CH)
509 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: CSV / ASCII-Delimited

  Alt 16. Apr 2015, 14:57
Vielleicht hilft dir das hier weiter:
Geschrieben für D7

Delphi-Quellcode:
// Deklarationen
Lines : array of TStrings;
procedure ReadCSV(AStrings : TStrings);

// Funktionen
procedure TForm1.ReadCSV(AStrings: TStrings);
var
  InString : boolean;

  CurrentLineIndex : integer;
  CurrentLine : string;

  CurrentCharIndex : integer;
  CurrentChar : char;

  StringBuffer : string;

  LLine : TStrings;
begin
  InString := False;
  StringBuffer := '';

  for CurrentLineIndex := 0 to AStrings.Count-1 do
  begin
    CurrentLine := AStrings[CurrentLineIndex];
    StringBuffer := '';
    LLine := TStringList.Create;

    for CurrentCharIndex := 1 to Length(CurrentLine) do
    begin
      CurrentChar := CurrentLine[CurrentCharIndex];

      if (not(InString)) and
         (CurrentChar = ' ') then
        Continue
      else if (not(InString)) and
              (CurrentChar = '"') then
      begin
        InString := true;
        Continue;
      end
      else if (InString) then
      begin
        if CurrentChar = '"then
        begin
          LLine.Add(StringBuffer);
          InString := false;
          StringBuffer := '';
          Continue;
        end;
      end;

      if (not(InString)) and
         (CurrentChar = ';') then
        Continue;

      StringBuffer := StringBuffer + CurrentChar;

      if StringBuffer = '[CRLF]then
      begin
        SetLength(Lines,Length(Lines)+1);
        Lines[Length(Lines)-1] := LLine;
        Break;
      end;
    end;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  LStrings : TStrings;
begin
  LStrings := TStringList.Create;
  LStrings := mmo1.Lines;
  ReadCSV(LStrings);
  ShowMessage(Lines[0][0]);
  ShowMessage(Lines[1][0]);
  ShowMessage(Lines[1][2]);
  ShowMessage(Lines[2][1]);
  ShowMessage(Lines[2][2]);
end;
Milos
  Mit Zitat antworten Zitat