Delphi之TStrings和TStringLists类

本文详细介绍了Delphi编程中TStrings和TStringLists类的功能和使用方法,包括如何通过这些类操纵字符串列表,以及与组件属性的交互。通过实例代码展示了如何创建和操作TStringLists类的实例,以及与不同组件属性的兼容性。此外,还提供了关键方法的解释和使用示例,帮助开发者更好地理解并应用这些类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


======================================================
注:本文源代码点此下载
======================================================

delphi之tstrings和tstringlists类

有些类不是组件,但它们支持存在的组件。这些类是其他组件的典型属性,直接由tpersistent派生,如tstrings、tcanvas和tcollection。

tstrings和tstringlists类

tstrings是一个抽象类,使你可以操纵属于组件(如tlistbox)的字符串列表。tstrings实际上并不管理字符串的内存(那是由拥有tstrings类的组件管理的),它只是定义了方法和属性来访问和操纵组件的字符串,而不是使用组件的win32 api函数和消息。

注意,我们所说的tstrings是一个抽象类。这就意味着tstrings并没有真正实现操纵字符串的代码,它只是定义了必要的方法有哪些。tstrings的派生组件实现了实际上的字符串操纵方法。

为了进一步说明这个点,举几个组件的例子,如tlistbox.items、tmemo.lines、和tcombobox.items等属性都是tstrings类型。你也许怀疑,如果它们的属性是tstrings类型,当这些属性的方法在代码中尚未实现时,怎么能调用这些方法呢?问得好。答案是,尽管这些属性被定义为tstrings类型,但这些属性的变量(例如设为tlistbox.fitems)是被实例化为派生类。为了阐明这点,设fitems是tlistbox的items属性的私有存储域:

tcostomlistbox = class(twincontrol)

private

fitem: tstrings;

注意虽然前述代码片段中的类类型是tcustomlistbox,但tlistbox是tcustomlistbox在同一单元中直接派生的,因此tlistbox可以访问它的私有域。

stdctrls.pas单元是delphi vcl的一部分,它定义了一个tstrings的派生类tlistboxstrings。 tlistboxstrings类的声明:

tlistboxstrings = class(tstrings)

private

listbox: tcustomlistbox;

protected

procedure put(index: integer; const s: string); override;

function get(index: integer): string; override;

function getcount: integer; override;

function getobject(index: integer): tobject; override;

procedure putobject(index: integer; aobject: tobject); override;

procedure setupdatestate(updating: boolean); override;

public

function add(const s: string): integer; override;

procedure clear; override;

procedure delete(index: integer); override;

procedure exchange(index1, index2: integer); override;

function indexof(const s: string): integer; override;

procedure insert(index: integer; const s: string); override;

procedure move(curindex, newindex: integer); override;

end;

stdctrls.pas接着实现了这个派生类的每一个方法。当tlistbox为其fitems变量创建它的类实例时,实际上就是创建这个派生类的实例并把它提供给fitems属性:

constructor tcostomlistbox.create(aowner: tcomponent);

begin

inherited create(aowner);

. . .

//创建一个tlistboxstrings实例

fitems := tlistboxstrings.create;

. . .

end;

这里要重申,虽然tstrings类定义了它的方法,但它并没有实现这些方法来操纵字符串。tstrings派生类实现了这些方法。对于组件编写者来说这一点很重要,因为你要知道如何能够像delphi组件那样来实现这个技术。通过vcl源代码来了解borland如何实现这些技术,在你迷惑的时候是非常必要的。

如果你不是组件编写者,但希望操纵字符串列表,可以使用另一个tstrings派生类——tstringlist,通过它你可以把一个完全独立的类实例化。tstringlist管理组件以外的字符串列表。最有用的是tstringlist与tstrings完全兼容。这意味着你能够直接把一个tstringlist实例赋给一个组件的tstrings属性。下列代码演示了如何创建一个tstringlist实例:

var

mystringlist: tstringlist;

begin

mystringlist := tstringlist.create;

要把字符串加入这个tstri n g l i s t实例,如下所示:

如果你要把同样的字符串加入到tmemo组件和tlistbox组件,所要做的就是利用不同组件的

tstrings属性的兼容性在代码中进行赋值:

这时用assign()方法复制tstrings实例,而不是像memo1.lines := mystringlist那样直接赋值。tstrings的一些常用方法

add(const s: string): integer把字符串s加入到字符串列表中,并返回字符串在列表中的位置

addobject(const s: string;aobject: tobject): integer把一个字符串和一个对象添加到一个字符串或字符串列表对象中

addstrings(strings: tstrings)从t s t r i n g s 中复制字符串到本字符串列表的后面

assign(source: tpersistent)用s o u r c e参数指定的字符串列表取代自己

clear从列表中删除所有字符串

delete(index: integer)删除由i n d e x指定位置的字符串

exchange(index1, index2: integer)交换由两个index值指定的字符串的位置

indexof(const s: string): integer返回字符串s在列表中的位置

insert(index: integer; const s: string)把字符串s插入到列表中由i n d e x指定的位置

move(curindex, newindex: integer)把curindex位置的字符串移动到newindex位置

loadfromfile(const filename: string)从文本文件filename中读取字符串列表

savetofile(const filename: string)把字符串列表保存到文本文件filename中


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值