Search code examples
arraysstringdelphistring-concatenationdelphi-2007

How to concatenate array of string elements into a string


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?


Solution

  • 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;