TStrings 类 简介

Tstrings类简单介绍及实例

在DELPHI的程序开发过程中Tstrings类的使用是比较频繁的,下面就此类在DELPHI5的开发环境中进行一下简单的介绍及实例(注:本文只对tstrings类中的方法及属性进行介绍,
从其父类继承的属性及方法不属本文讨论之内)。
Add
原型:function Add(const S: string): Integer; virtual;
注解:此方法是在字符表中的后面添加字符串,返回值是新添加字符串的索引值。
实例:
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=1 to 10 do
listbox1.Items.Add(inttostr(i));
end;

AddStrings
原型:procedure AddStrings(Strings: TStrings); virtual;
注解:从字符表中添加一组字符表
实例:
procedure TForm1.Button2Click(Sender: TObject);
var
abc:tstringlist;
begin
abc:=tstringlist.Create;
abc.Assign(listbox1.Items);
listbox2.Items.AddStrings(abc);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=1 to 10 do
listbox1.Items.Add(inttostr(i));
end;

Append
原型:procedure Append(const S: string);
注解:此方法是在字符表中的后面添加字符串。
实例:
procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.Items.Append(’you will win!!’);
end;

Assign
原型:procedure Assign(Source: TPersistent); override;
注解:用此方法从另一个兼容的对象中的值赋给此对象,相当于复制和赋值的功能
实例:
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=1 to 10 do
begin
listbox1.items.add(inttostr(i));
end;
listbox2.items.Assign(listbox1.items);
end;

BeginUpdate
原型:procedure BeginUpdate;
注解:此方法是在字符串列表与可视化列表控件进行操作时使用的,
目的是防止添加或删除item时进行刷新(在大数据量进行添加或删除操作时是很费时间的)。
与EndUpdate进行配对操作。
实例:
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
listbox1.Items.BeginUpdate;
for i:=0 to 10000 do
begin
listbox1.items.Add(inttostr(i));
end;
listbox1.Items.EndUpdate;
end;

Clear
原型:procedure Clear; virtual; abstract;
注解:此方法清除字符表中全部的内容
实例:
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to 10 do
listbox1.items.add(inttostr(i));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.Items.Clear;
end;

Delete
原型:procedure Delete(Index: Integer); virtual; abstract;
注解:此方法通过索引值删除指定的字符串
实例:
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to 10 do
listbox1.items.add(inttostr(i));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.Items.Delete(listbox1.ItemIndex);
end;

Destroy
原型:destructor Destroy; override;
注解:消毁一个TStrings类的实例。
实例:
var
Form1: TForm1;
aaa:boolean;
bbb:tstringlist;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
aaa:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if aaa=true then
begin
bbb:=tstringlist.create;
aaa:=false;
end
else
begin
bbb.Destroy;
aaa:=true;
end;
end;

end.


EndUpdate
原型:procedure EndUpdate;
注解:此方法是在字符串列表与可视化列表控件进行操作时使用的,目的是防止添
加或删除item时进行刷新(在大数据量进行添加或删除操作时是很费时间的)。
与BeginUpdate进行配对操作。
实例:
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
listbox1.Items.BeginUpdate;
for i:=0 to 10000 do
begin
listbox1.items.Add(inttostr(i));
end;
listbox1.Items.EndUpdate;
end;

Equals
原型:function Equals(Strings: TStrings): Boolean;
注解:此方法为判断两个Tstrings类中的内容是否相当,如果相等返回为true,不等返回为false。
实例:
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to 10 do
begin
listbox1.items.add(inttostr(i));
listbox2.items.add(inttostr(i));
end;

for i:=0 to 9 do
begin
listbox3.items.add(inttostr(i));
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if listbox1.items.Equals(listbox2.items) then
showmessage(’相等’)
else
showmessage(’不相等’);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if listbox2.items.Equals(listbox3.items) then
showmessage(’相等’)
else
showmessage(’不相等’);
end;

Exchange
原型:procedure Exchange(Index1, Index2: Integer); virtual;
注解:此方法是借助两个字符串在表中的索引而调换位置。与move区别是(If either string has an associated object, Exchange changes the position of the object as well.如果这个字符串有一个链接的对象,那么这个对象的位置也随着字符串的位置改变而改变。)
实例:
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 10 do
listbox1.items.add(inttostr(i));

listbox1.items.Exchange(0,10);
end;

GetText
原型:function GetText: PChar; virtual;
注解:取得所有此类中的字符。
实例:
procedure TForm1.FormCreate(Sender: TObject);
begin
listbox1.Items.add(’1’);
listbox1.Items.add(’1’);
listbox1.Items.add(’1’);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(string(listbox1.Items.GetText));
end;

IndexOf
原型:function IndexOf(const S: string): Integer; virtual;
注解:通过字符串常量来得到在此类中的索引值。
实例:
procedure TForm1.FormCreate(Sender: TObject);
begin
listbox1.Items.add(’1’);
listbox1.Items.add(’2’);
listbox1.Items.add(’3’);
listbox1.Items.add(’4’);
listbox1.Items.add(’5’);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(inttostr(listbox1.Items.indexof(’5’)));
end;

Insert
原型:procedure Insert(Index: Integer; const S: string); virtual; abstract;
注解:在指定的索引位置插入一个字符串。
实例:
procedure TForm1.FormCreate(Sender: TObject);
begin
listbox1.Items.add(’1’);
listbox1.Items.add(’2’);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.Items.Insert(0,’abc’);
end;

LoadFromFile
原型:procedure LoadFromFile(const FileName: string); virtual;
注解:从指定的文件名装载文件内容。
实例:
procedure TForm1.Button2Click(Sender: TObject);
begin
listbox1.items.LoadFromFile(’c:/abc.txt’);
end;

LoadFromStream
原型:procedure LoadFromStream(Stream: TStream); virtual;
注解:从流中装载文件。
实例:
procedure TForm1.Button1Click(Sender: TObject);
var
abc:tmemorystream;
begin
abc:=tmemorystream.create;
abc.LoadFromFile(’c:/csdn.txt’);
memo1.lines.LoadFromStream(abc);
end;


Move
原型:procedure Move(CurIndex, NewIndex: Integer); virtual;
注解:交换两个以索引为参数的字符串。与Exchange不同的是
(If the string has an associated object, the object remains
 associated with the string in its new position.如果这个字符串有一个对象,
那么这个对象的位置不变,自动链接到新的字符串)
实例:
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.Move(ListBox1.ItemIndex, 0);
end;

SaveToFile
原型:procedure SaveToFile(const FileName: string); virtual;
注解:通过参数来把tstrings中的内容存成文件。
实例:
procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.Lines.SaveToFile(’c:/abc.txt’);
end;

SaveToStream
原型:procedure SaveToStream(Stream: TStream); virtual;
注解:保存成流。
实例:
procedure TForm1.Button1Click(Sender: TObject);
var
abc:tstringlist;
abcd:tmemorystream;
begin
abc:=tstringlist.create;
abcd:=tmemorystream.create;
abc.add(’1’);
abc.add(’1’);
abc.add(’1’);
abc.add(’1’);
abc.SaveToStream(abcd);
abcd.Position:=0;
memo1.lines.loadfromstream(abcd);
end;


SetText
原型:procedure SetText(Text: PChar); virtual;
注解:设置tstrings类中的内容。
实例:
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to 10 do
begin
listbox1.items.add(inttostr(i));
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.Items.SetText(pchar(’aa’));
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值