在delphi中,TStringList使用是较为广泛的。花了一些时间,钻研了下TStringList的用法。好的,废话不多说了,让我们进入程序世界里吧!
开发环境:Delphi7
开发使用控件截图如下:
开发源代码Unit2.pas如下:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
i:Integer;
StrList,StrList1,StrList2,StrList3,StrList4:TStringList;
implementation
{$R *.dfm}
function DescCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result :=-AnsiCompareText(List[Index1], List[Index2]);
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
StrList:=TStringList.Create; //创建对象
StrList.Add('a'); //add
StrList.Add('aa');
StrList.Add('aaa');
for i:=0 to StrList.Count-1 do
begin
showmessage('显示字符串'+StrList.Strings[i]);
end;
StrList.Exchange(0,2); //置换
for i:=0 to StrList.Count-1 do
begin
showmessage('显示字符串'+StrList.Strings[i]);
end;
StrList.Exchange(0,2); //换完之后再换回来
for i:=0 to StrList.Count-1 do
begin
showmessage('显示字符串'+StrList.Strings[i]);
end;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
StrList1:=TStringList.Create;
StrList1.Add('Jack');
StrList1.Add('Mary');
StrList1.Add('Nancy');
StrList1.Insert(0,'study'); //注意insert位置放在for循环前
StrList1.Insert(1,'cyd');
// StrList1.Sort;//排序
StrList1.Sorted:=True;//指定排序,功能与Sort用法一样
for i:=0 to StrList1.Count-1 do
begin
showmessage('插入和排序的使用'+StrList1.Strings[i]);
end;
// i:=StrList1.IndexOf('学习2');
for i:=0 to StrList1.Count-1 do
begin
showmessage('倒排序函数使用前'+StrList1.Strings[i]);
end;
StrList1.CustomSort(DescCompareStrings); //调用倒排序函数
for i:=0 to StrList1.Count-1 do
begin
showmessage('倒排序函数使用后'+StrList1.Strings[i]);
end;
showmessage(Strlist1.Values['study']);
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
StrList2:=TStringList.Create;
StrList3:=TStringList.Create;
StrList2.Add('ccomputer');
StrList2.Add('book');
StrList3.Add('wsq');
StrList3.Add('cyd');
showmessage(StrList2.Values['book']);
showmessage(StrList2.ValueFromIndex[0]);//表示从第1个字符起,从第二个字符截取
showmessage(inttostr(StrList2.Count));//总数
showmessage(StrList2.Text);
StrList2.Delete(0); //删除
showmessage(StrList2.Text);
StrList2.Clear; // 程序运行效果等价于 showmessage(StrList2.Values['book'])
showmessage('此时已经经过clear了'+StrList2.Text);
StrList2.AddStrings(StrList3);
for i:=0 to StrList2.Count-1 do
begin
showmessage('此时已经经过增添了字符串操作'+StrList2.Strings[i]);
end;
end;
procedure TForm2.Button4Click(Sender: TObject);
begin
StrList2:=TStringList.Create;
StrList4:=TStringList.Create;
StrList2.Add('ccomputer');
StrList2.Add('book');
StrList4.Add('delphi');
StrList4.Add('java');
StrList4.Add('python');
if StrList4.Equals(StrList2)=true then
begin
showmessage('Equal判断是否相等'+'匹配成功');
end
else
begin
showmessage('Equal判断是否相等'+'匹配失败');
end;
StrList4.Delimiter:='*'; //分割符
//StrList4.DelimitedText:='a.b.c.d.e.f'; //有分割符的字符串
for i:=0 to StrList4.Count-1 do
begin
Edit1.Text:=StrList4.DelimitedText; //有分割符的字符串
end;
{ StrList4.Free; //释放
showmessage('释放后的'+StrList4.Strings[i]); }
//StrList4.Destroy; //析构
//StrList4.LoadFromFile('J:/filelist.tx'); //打开
//StrList4.SaveToFile('I:/TstringList.txt'); //保存
StrList4.Free;
StrList4:=nil;
end;
end.
到此,相信大家都对TStringList的运用已经掌握了,坚持,就是胜利,共同进步,早日成为高级程序员,希望你也是,我也是。