- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi 利用TDictionary 文本去重
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Generics.Collections; {Delphi 2009 新增的泛型容器单元}
var
Dictionary: TDictionary;
//www.delphitop.com
procedure TForm1.Button1Click(Sender: TObject);
var
key:string;
pair: TPair;
begin
//自动去重复
Dictionary.AddOrSetValue('key1', 'value1');
Dictionary.AddOrSetValue('key1', 'value2');
{遍历看看}
{
for pair in Dictionary.Keys do
begin
Memo1.Lines.Add(Dictionary[pair.Key]); //得到得结果都是不重复得
end; }
for key in Dictionary.Keys do
begin
Memo1.Lines.Add(key); //得到得结果都是不重复得
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Dictionary := TDictionary.Create;
end;
end.