1. Home
  2. Computing & Technology
  3. Delphi Programming

How to merge strings in two TStringList's

By Zarko Gajic, About.com

The following example code updates the strings in a list box given the strings contained in another list box. While merging, if the destination list box does NOT contain a string from the source list, the string will be added.

Usage (add non-existing items from ListBox2 to ListBox1) :

MergeStrings(ListBox1.Items, ListBox2.Items) ;

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

Delphi tips navigator:
» Hide caret (text cursor) "inside" TMemo component
« Hiding the cursor from the screen

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2003 Delphi Tips
  7. How to merge strings in two TStringList's

©2009 About.com, a part of The New York Times Company.

All rights reserved.