开发者

delphi array of string stringlist conversion

Is there a simple way in delphi to convert an array of strings to a tst开发者_开发问答ringlist?


Once you have created the string list, you can simply call AddStrings().

Or for older versions of Delphi that do not support the AddStrings() overloads that accept arrays, you can roll your own.

function StringListFromStrings(const Strings: array of string): TStringList;
var
  i: Integer;
begin
  Result := TStringList.Create;
  for i := low(Strings) to high(Strings) do
    Result.Add(Strings[i]);
end;

Using an open array parameter affords the maximum flexibility for the caller.


For pre-generic versions of Delphi, you can use something like this:

type
  TStringArray = array of string;

procedure StringListFromStrings(const StringArray: TStringArray; 
  const SL: TStringList);
var
  // Versions of Delphi supporting for..in loops
  s: string;

  // Pre for..in version
  // i: Integer;
begin
  // TStringList should be created and passed in, so it's clear
  // where it should be free'd.
  Assert(Assigned(SL));

  // Delphi versions with for..in support
  for s in StringArray do
    SL.Add(s);

  // Pre for..in versions
  // for i := Low(StringArray) to High(StringArray) do
  //   SL.Add(StringArray[i]);
end;


With recent Delphi version you can call "addStrings" method of a TStrings and TstringList classes like this:

function StringListFromStrings(const Strings: array of string): TStringList;
var
  i: Integer;
begin
  Result := TStringList.Create;
  Result.addStrings(Strings);
end;

Yuo can find documentation about this method at: https://docwiki.embarcadero.com/Libraries/Sydney/en/System.Classes.TStrings.AddStrings

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜