返回

TStringList类 (TObject->TPersistent->TStrings)

Unit:Classes

简述:TStringList维护一个字符串列表。

描述:
使用字符串列表对象存储和操作字符串列表。TStringList实现了TStrings引入的抽象属性和方法,并引入了新的属性、事件和方法
对列表中的字符串进行排序。
禁止排序列表中的重复字符串。
响应列表内容中的更改。
控制字符串是否以区分大小写或不区分大小写的方式定位、排序和标识为重复字符串。

代码示例:

本例使用表单上的列表框和标签。当应用程序运行时,将创建一个字符串列表对象,并向其中添加三个字符串。Find方法搜索字符串以查找与该字符串的匹配项慒降低?如果找到该字符串,字符串列表中的所有字符串都将添加到列表框中,并且lowers?字符串的索引值将显示在label控件的标题中。

void __fastcall TForm1::Button1Click(TObject *Sender)

{
int Index;
TStringList* MyList = new TStringList();
try
MyList->Add("Animals");
MyList->Add("Flowers");
MyList->Add("Ocean");
MyList->Sort(); // Find will only work on sorted lists
if (MyList->Find("Flowers", Index))
{
ListBox1->Items->AddStrings(MyList);
Label1->Caption = "Flowers has an index value of " + AnsiString(Index);
}
__finally
{
delete MyList;

}
}