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<string,string>;
//www.delphitop.com

procedure TForm1.Button1Click(Sender: TObject);
var
key:string;
pair: TPair<string,string>;
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<string,string>.Create;

end;

end.

相关阅读 >>

Delphi tstrings 随机打乱

Delphi串口通信编程

indy tidtcpclient 在网络掉线时的处理方法

Delphi 把流中的字符串转换为 utf 格式

Delphi 获取当前目录下所有文件名3

Delphi listview的用法

Delphi opendialog1文件过滤类型

Delphi中使用spcomm来实现串口通讯

Delphi xe2 - 万一“获取程序自身大小的函数”改进版

Delphi 服务器与客户端的时间同步

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...