3

我有一个包含 10,000 项和许多重复项的列表框!我想把它保存到一个没有重复项目的文件(一个项目而不是所有副本!)我使用这种方式:

Function TMain.List_ExistsIn(ListBox_NAme: TListBox; EParameter: String): Integer;
Var
  i: Integer;
Begin
  EParameter := LowerCase(EParameter);
  Result := -1;
  For i:=0 To ListBox_Name.Items.Count - 1 Do
    If EParameter = Lowercase(ListBox_Name.Items[i]) Then Begin
      Result := i;
      Break;
    End;
End;

我使用上面的代码来检测现有项目并按照程序保存它:

Procedure TMain.MakeList(ListBox_Name: TListBox; FileName: String); //================
Var
  i: Integer;
  Temp_ListBox: TListBox;
Begin
  Temp_ListBox := TListBox.Create(Main);
  With Temp_ListBox Do Begin
    Parent := Main;
    Clear;
    For i:=0 To ListBox_Name.Count - 1 Do
      If Main.List_ExistsIn(Temp_ListBox, ListBox_Name.Items[i]) = -1 Then
        Items.Add(ListBox_Name.Items[i]);
    Items.SaveToFile(FileName);
    Free;
  End;
End;

但这需要很长时间才能进行。有没有更好更快捷的方法?谢谢。

4

2 回答 2

7

试试这个

procedure TForm1.FormCreate(Sender: TObject);
var
  StrList: TStringList;
  I: Integer;
begin
  StrList := TStringList.Create;
  StrList.Sorted := True;
  StrList.Duplicates := dupIgnore;
  StrList.AddStrings(ListBox1.Items);  //Your List Box Items
  StrList.SaveToFile('C:\abc.txt');
  StrList.Free; //Cleanup
end;
于 2010-06-21T09:48:21.360 回答
1

注意臭名昭著的 CompareString() 效果...

将 59A、5-9A、59-A、-59-A 插入排序列表 1。列表变为 59A、-59-A、5-9A、59-A 和 .Find() 或 .IndexOf() 将失败找到 59-A。

现在将相同的值插入排序列表 2,但按 59A、-59-A、5-9A、59-A 的顺序插入。列表变为 59A、59-A、-59-A、5-9A。.Find() 和 .IndexOf() 可以定位 59-A。

有关更多详细信息,请参阅此博客

于 2010-06-25T04:51:57.487 回答