Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
How do you add or combine two TStringList objects to eachother? eg.

Delphi
var
  List1, List2, List3 : TStringList;
begin
  List1.Add('Line1List1');
  List1.Add('Line2List1');
  List2.Add('Line1List2');
  List2.Add('Line2List2');
end


are the two lists with respective strings. How do I combine them into a single list?
Like List3 having:
Delphi
'Line1List1'
'Line2List1'
'Line1List2'
'Line2List2'

which is (List1 + List2).

How can I do this? No help found...
Posted
Comments
Sergey Alexandrovich Kryukov 1-Jul-12 3:50am    
Is this +, or disjunction?
Damith is right, that's why the like with IndexOf -- it exclude duplicates, but this check can be optional, to make just '+'.
--SA

You can set the Duplicate method of TStringList object with dupIgnore :

Delphi
procedure CombineStrings(S1, S2, CombinedList: TStringList);
begin
  S1.Duplicates := dupIgnore;
  S1.Sorted := True;
  S1.AddStrings(S2);
  CombinedList := S1;
end;


Sample :

Delphi
var
  SList1, SList2: TStringList;
begin
  SList1 := TStringList.Create;
  SList2 := TStringList.Create;
  try
    SList1.Add('Item 1');
    SList1.Add('Item 2');
    SList2.Add('Item 1');
    SList2.Add('Item 2');
    SList2.Add('Item 3');

    CombineStrings(SList1, SList2, SList1);

    ListBox1.Items.Assign(SList1);
  finally
    SList1.Free;
    SList2.Free;
  end;
end;
 
Share this answer
 
SQL
procedure MergeStrings(Dest, Source: TStrings) ;
var j : integer;
begin
   for j := 0 to -1 + Source.Count do
     if Dest.IndexOf(Source[j]) = -1 then
       Dest.Add(Source[j]) ;
end;


http://delphi.about.com/cs/adptips2003/a/bltip0703_4.htm[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Jul-12 3:48am    
Right, a 5.
--SA
Sergey Alexandrovich Kryukov 1-Jul-12 3:51am    
The check ("IndexOf") could be optional...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900