合并或将两个TStringList一起添加 [英] Combine or Add two TStringLists together

查看:101
本文介绍了合并或将两个TStringList一起添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将两个TStringList对象彼此添加或组合?例如.

How do you add or combine two TStringList objects to eachother? eg.

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



是带有相应字符串的两个列表.如何将它们合并为一个列表?
像具有以下内容的List3:



are the two lists with respective strings. How do I combine them into a single list?
Like List3 having:

'Line1List1'
'Line2List1'
'Line1List2'
'Line2List2'


这是(List1 + List2).

我怎样才能做到这一点?找不到帮助...


which is (List1 + List2).

How can I do this? No help found...

推荐答案

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 [ ^ ]



http://delphi.about.com/cs/adptips2003/a/bltip0703_4.htm[^]


您可以使用dupIgnore设置TStringList对象的Duplicate方法:

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

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



样本:



Sample :

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;


这篇关于合并或将两个TStringList一起添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆