delphi array of string stringlist conversion

xianghua picture xianghua · May 4, 2011 · Viewed 18.5k times · Source

Is there a simple way in delphi to convert an array of strings to a tstringlist?

Answer

David Heffernan picture David Heffernan · May 4, 2011

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.