2
votes

I need to read each line in memo1.lines and perform a specific action on it. I tried this:

procedure TForm1.Button2Click(Sender: TObject);
var
  zoznam: TStringList;
  Line: string;
  i       : Integer;
begin
  zoznam := TStringList.Create;
  zoznam.Assign(Memo1.Lines);
  for i := 0 to zoznam.Count-1 do
  begin
    edit1.Text := Memo1.Text;
    Button1.Click;
  end;
end;

However, it takes every line into one string, and I want to read each line separately (read a line, do something, read the next line, do something, ...)

1

1 Answers

6
votes

TStrings has a Strings[] property for accessing individual strings:

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  Line: string;
  zoznam: TStringList;
begin
  zoznam := TStringList.Create;
  try
    zoznam.Assign(Memo1.Lines);
    for i := 0 to zoznam.Count-1 do
    begin
      Line := zoznam.Strings[i]; // or: Line := zoznam[i];
      //...
    end;
  finally
    zoznam.Free;
  end;
end;

Or, you could just loop through the Memo lines directly:

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  Line: string;
begin
  for i := 0 to Memo1.Lines.Count-1 do
  begin
    Line := Memo1.Lines.Strings[i]; // or Line := Memo1.Lines[i];
    //...
  end;
end;