将字符串中的数字列表转换为数组

0 delphi delphi-xe3

我在.txt文件(由Delphi读取的单个字符串)中给出21个y坐标的函数,这些坐标是在.txt文档中垂直写入的.如何将此字符串拆分为单个值并将其存储在数组"Farr"中?文本文档内容如下所示:

-3
-2.5
-2
...
Run Code Online (Sandbox Code Playgroud)

每个值都需要存储到0 ... 21的常规数组中,如下所示:

Farr:=[-3, -2.5, -2, ...]
Run Code Online (Sandbox Code Playgroud)

谢谢!

Sti*_*ers 6

试试这个:

var
  sl: TStringList;
  i: integer;
  s: string;
  arr: array of double;
begin
  sl := TStringList.Create;
  try
    sl.LoadFromFile(FilePath);
    SetLength(arr, sl.Count);
    for i := 0 to sl.Count - 1 do
      arr[i] := StrToFloat(sl[i]);
  finally
    sl.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)