How to write an equivalent to PHP implode(string $separator, array $array): string in Pascal

1 Answer

0 votes
program ImplodeDemo;

function Implode(separator: string; elements: array of string): string;
var
  i: Integer;
  resultStr: string;
begin
  resultStr := '';
  for i := Low(elements) to High(elements) do
  begin
    if i > Low(elements) then
      resultStr := resultStr + separator;
    resultStr := resultStr + elements[i];
  end;
  Implode := resultStr;
end;

var
  words: array[0..5] of string;
  output: string;
begin
  words[0] := 'May';
  words[1] := 'the';
  words[2] := 'Force';
  words[3] := 'be';
  words[4] := 'with';
  words[5] := 'you';

  output := Implode('-', words);
  
  Writeln(output);  { Output: May-the-Force-be-with-you }
end.



(*
run:

May-the-Force-be-with-you

*)

 



answered Dec 5, 2025 by avibootz

Related questions

...