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 移位函数

Delphi winapi: getmodulehandle - 获取一个模块(exe 或 dll)的句柄

Delphi 用代码实现为程序创建快捷方式的二种方法

Delphi 相对路径和绝对路径的转换

Delphi 关闭xp保护 替换explorer.exe

Delphi vclunzip组件解压缩文件用法

Delphi读ios的设备id

Delphi 命令行程序调用单元函数

Delphi ifileoperation替换shfileoperation

Delphi读取一个access数据库中的表名

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



打赏

取消

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

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

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

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

评论

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