Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Pascal

Idiom #53 Join a list of strings

Concatenate elements of string list x joined by the separator ", " to create a single string y.

Turning the strings "eggs", "butter", "milk" into the string "eggs, butter, milk"
var
  _x: Array of string;
  _y: String;
  i: Integer;
begin
  _y := ''; //initialize result to an empy string
  // assume _x is initialized to contain some values
  for i := Low(_x) to High(_x) do
  begin
    _y := _y + _x[i];
    if i < High(_x) then _y := _y + ';';
  end;
end;
with Ada.Containers.Indefinite_Vectors; use Ada.Containers;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
declare
   Last : Cursor := X.Last;
   Y : Unbounded_String;
         
begin
   for C in X.Iterate loop
      Y := Y & Element (C) & (if C = Last then "" else ", ");
   end loop;
end;

New implementation...