如何将文本文件的交替行拆分为两个数组?

cod*_*101 1 delphi

如何将文本文件中的数据读入两个数组?一个是字符串,另一个是整数?

文本文件的布局如下:

 Hello
 1
 Test
 2
 Bye
 3
Run Code Online (Sandbox Code Playgroud)

每个数字对应于它上面的文本.任何人都可以帮助我吗?非常感谢它

Gol*_*rol 6

var
  Items: TStringList;
  Strings: array of string;
  Integers: array of Integer;
  i, Count: Integer;
begin
  Items := TStringList.Create;
  try
    Items.LoadFromFile('c:\YourFileName.txt');
    // Production code should check that Items.Count is even at this point.

    // Actual arrays here. Set their size once, because we know already.
    // growing your arrays inside the iteration will cause many reallocations
    // and memory fragmentation.
    Count := Items.Count div 2;
    SetLength(Strings, Count);
    SetLength(Integers, Count);

    for i := 0 to Count - 1 do
    begin
      Strings[i] := Items[i*2];
      Integers[i] := StrToInt(Items[i*2+1]);
    end;
  finally
    Items.Free;
  end;
end
Run Code Online (Sandbox Code Playgroud)

  • 实际上使用`shl`来倍增让我看起来很愚蠢! (2认同)