Delphi 不重覆的字串列 TStringList 试作
环境:Delphi RAD 10.4
动机:
不重覆字串对于Python是很简单的,只须把List 丢给Set( )就能自动排序、去掉重覆者。
今天实作一个Delphi的类似功能,应用的是:
List.Sorted := True;
List.Duplicates := dupIgnore;
画面:
程式目地: 让comboBox1 从TXT档载入,可在edit1新增,程式结束再存回TXT档。
其中主要的功能包含:
TStringList.LoadFromFile
TStringList.SaveToFile
TString.Duplicates
说明:
文字档filterHx.Txt 必需放在 Win32\Debug内 ,因为RAD10 会把EXE档放在这里。
全部程式码:
unit testStringList01;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; ComboBox1: TComboBox; function sUnique(List:TStringList; newOne: string):TStringList; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Button1Click(Sender: TObject); procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); private { Private declarations } public { Public declarations } end;var Form1: TForm1; uString : TStringList;implementation{$R *.dfm}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin uString.SaveToFile('filterHx.txt');end;procedure TForm1.FormCreate(Sender: TObject);var i : integer;begin uString := TStringList.Create; //--- 编码时,此档应放在 win32\debug内 uString.LoadFromFile('filterHx.txt'); comboBox1.Items.Assign(uString);end;//--- TStringList 重整 排序 不重覆function TForm1.sUnique(List:TStringList; newOne: string):TStringList;begin //--- Ignore if duplicated List.Sorted := True; List.Duplicates := dupIgnore; List.Add(newOne); result := List;end;procedure TForm1.Button1Click(Sender: TObject);var tmp : string; i : integer;begin tmp := edit1.Text; if length(tmp) <>0 then begin sUnique(uString,tmp); comboBox1.Items.Assign(uString); end;end;procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);begin if Key = 13 then Button1Click(nil);end;end.