How to concatenate array of string elements into a string

Hwau picture Hwau · May 10, 2015 · Viewed 7.6k times · Source

How should an array of string be converted into string (With separator char)? I mean, is there some system function I can use instead of writing my own function?

Answer

Jens Borrisholt picture Jens Borrisholt · May 11, 2015

Since you are using Delphi 2007 you have to do it you self:

function StrArrayJoin(const StringArray : array of string; const Separator : string) : string;
var
  i : Integer;
begin
  Result := '';
  for i := low(StringArray) to high(StringArray) do
    Result := Result + StringArray[i] + Separator;

  Delete(Result, Length(Result), 1);
end;

Simply traverse the array and concat it with your seperator.

And a small test example:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption :=StrArrayJoin(['This', 'is', 'a', 'test'], ' ');
end;