Delphi之TStrings和TStringLists类

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中