我可以读取一个文件将其加载到StringList中,但之后我想要反转其内容,将其向后复制到第二个StringList。我需要帮助向后填充第二个StringList。
procedure TForm1.btnsteClick(Sender: TObject);
var
Lista01, Lista02 : TStringList;
i, NumeroLinhas:Integer;
UltimaLinha :string;
begin
// Create the temporary StringList
Lista01 := TStringList.Create;
Lista02 := TStringList.Create;
// Load the content of the input file
Lista01.LoadFromFile('C:\entrada.txt');
try
// Loop through the StringList
for i:=0 to Lista01.Count -1 do
begin
// Show the content of the line
//ShowMessage('linha ' + IntToStr(i) + ' é ' + Lista01[i]);
Lista01[i] := Lista01[i];
end;
// Retrieve the number of lines
NumeroLinhas := Lista01.Count;
// Retrieve last line
UltimaLinha := Lista01[NumeroLinhas -1];
for i:=0 to Lista01.Count -1 do
begin
// I want to copy backwards Lista01 into List02
// Lista02[i] := "";
end;
// Save on file the modifications
Lista01.SaveToFile('C:\saida1.txt');
Lista02.SaveToFile('C:\saida2.txt');
finally
// Release memory
FreeAndNil(Lista01);
FreeAndNil(Lista02);
ShowMessage('Sic');
end;
end;
答案 0 :(得分:3)
此代码将执行您要查找的内容,以相反的顺序将Lista01的内容填入Lista02。
for i := Lista01.Count - 1 downto 0 do begin
Lista02.Add(Lista01[i]);
end;