- 浏览: 59143 次
- 性别:
- 来自: 苏州
文章分类
最新评论
Delphi TStrings 用法
对于TStrings类型变量,需要使用TStringList.Create;创建[luther.gliethttp],否则提示 abstract error错误
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的。
常规的用法大家都知道,现在来讨论它的一些高级的用法。
先把要讨论的几个属性列出来:
1、CommaText
2、Delimiter & DelimitedText
3、Names & Values & ValueFromIndex
先看第一个:CommaText。怎么用呢?用代码说话:
const
constr :String = 'aaa,bbb,ccc,ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.CommaText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
end;
执行了这段代码后,可以看到ShowMessage显示出来的分别是:aaa bbb ccc ddd。
也就是说,strs.CommaText := constr这一句的作用,就是把一个字符串以','为分割符,分段添加到TStrings中。
那么如果不是以','来分割,又该怎么做呢?现在看第二个例子。使用Delimiter和DelimitedText。
const
constr :String = 'aaa\bbb\ccc\ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
end;
可以看到, 显示的效果和第一个例子是一模一样的。解释一下:
Delimiter为分隔符,默认为:','。DelimitedText就是按Delimiter为分隔符的一个串,得到赋值后回把这个字符串按Delimiter的字符添加到TStrings中。
说到这里,有想起一个属性,QuoteChar。其默认值为:'"'(不包括单引号)
有何用呢?看例子:
const
constr :String = '"aaa"\"bbb"\"ccc"\"ddd"';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
end;
显示出来的仍然是aaa bbb ccc ddd。为什么不是:"aaa" "bbb" "ccc" "ddd"呢?
再来看一个例子:
const
constr :String = '|aaa|\|bbb|\|ccc|\|ddd|';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.QuoteChar := '|';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
end;
显示出来的又是aaa bbb ccc ddd。对比一下,应该不难明白吧?这个就不多说了,用得也不多。
但是还要多说一句,当Delimiter为:','而QuoteChar为:'"'时,DelimitedText和CommaText是同等的。
最后要说的三个是:Names & Values & ValueFromIndex。
看看下面的代码:
const
constr :String = '0=aaa,1=bbb,2=ccc,3=ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.CommaText := constr;
for i := 0 to strs.Count-1 do
begin
ShowMessage(strs.Names[i]);
ShowMessage(strs.Values[strs.Names[i]]);
ShowMessage(strs.ValueFromIndex[i]);
end;
end;
通过这个例子不难看出:
这个时候strs中的内容是:
0=aaa
1=bbb
2=ccc
3=ddd
而Names中则是:
0
1
2
3
在Values中则是:
aaa
bbb
ccc
ddd
这几个属性用来处理ini文件是非常好用的。
另外说一句,在Delphi6下似乎没有ValueFromIndex这个属性
发表评论
-
TIniFile
2012-03-20 14:13 880Delphi TIniFile 学习 一、定义 ... -
delphi QuickReport
2012-03-20 14:54 1856报表控件简介 2009-03-16 ... -
Delphi中ClientDataSet的数据查找
2012-01-03 19:03 1289ClientDataSet的数据查找。我所介绍的心得和技巧都是 ... -
Delphi中ClientDataSet浅析
2012-01-03 19:01 1089与一般的数据 ... -
Delphi中ClientDataSet的用法
2012-01-03 18:58 1850TClientDataSet控件继承自TDataS ... -
Delphi 6 程序员代码编写标准指南
2011-12-28 11:06 1000TControl ... -
iplot的操作
2011-10-27 11:04 1072procedureTForm1.iplot_init(); b ... -
delphi控件使用
2011-10-27 11:07 3803Standard页 1- Tlistbox控件 重要属性 ... -
DELPHI中操作ACCESS数据库
2011-10-27 12:14 562DELPHI中操作ACCESS数据库(建立.mdb文件,压缩数 ... -
Treeview用于显示按照树形结构进行组织的数据
2011-10-27 12:21 678Treeview用于显示按照树形结构进行组织的数据。 Tr ... -
delphi TMemoryStream对象用法
2011-11-01 09:08 1663TMemoryStream对象 TMemoryStrea ... -
Delphi 中流的使用
2011-11-01 09:37 838所谓"流", 就是 ... -
delphi TStrings 管理对象的属性
2011-11-02 16:12 694Delphi中有关TStrings 管理对象的用法心得 sl ...
相关推荐
关于 Delphi字符串、数组的函数,包含【Copy】、【Concat】等函数
json4delphi JSON for Delphi,支持较早版本的Delphi(6或更高版本) 对象-pascal本机代码,仅使用TList,TStrings和TStringList类例子: var Json: TJson; Str: Stringbegin Json := TJson.Create(); // put Json....
delphi:子符串替换函数 delphi:TStrings的详细用法
执行DOS命令的DELPHI控件: property StdOutRead :THandle read fOutputRead; property StdInWrite :THandle read fInputWrite; property StdErrRead :THandle read fErrorRead; procedure ...
function TableToClassInfo(ATableName: string): TStrings; //取得SQLServer字段的类型,返回相应Delphi变量类型 function GetColumnType(AType: integer): string; //取得SQLServer字段的类型,返回...
Delphi动态建立数据库别名的源码实例,源码包中一共介绍了4种在Delphi程序中动态地建立和使用别名的方法,每种方法都附有完整的源码文件,AliasN为数据库别名,DbLocat为数据库别名对应的目录,DriverN为驱动类型,对...
vs2005自定义控件,实现了delphi中TStrings(TStringList)一样的效果
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的。 常规的用法大家都知道,现在来讨论它的一些高级的用法。
也就是namespace,使用Delphi的uses关键字。 3、IDE支持Delphi语言和C# 以前听说还支持VB.NET,可能是被CUT了。 4、IDE新增重构(ReFactoring)的一些功能 如ExtractMethod、Rename,很新鲜,也...
在Delphi中,对对象的调用实际上采用了两个步骤,首先是把对象地址放入eax寄存器中,然后call类方法,所以如果不使用继承类增加数据的话,用父类调用继承类的方法是没问题的,所以其实这样的方法在D7中也可以使用,...
Delphi编写的日志记录组件源代码,从中可以学习自定义组件的一些技巧,如注册到组件面板、添加自定义图标等。 日志组件具有以下属性及方法: public { Public declarations } procedure AddLog(p_Info: string); /...
procedure FreeStringsObjects(SL: TStrings); //系统处理止 //时间处理起 //整数到时间 function TimeT_To_DateTime(TimeT: Longint): TDateTime; //转化为秒 function TimeToSecond(const H, M, S: ...
procedure GetDisks(Strings: TStrings { TStringList ??? }); //获取所有盘符 procedure HideApp; //隐藏程序 function GetTmpPath: String; //取得WINDOWS的Temp路径 function GetSysPath: String; //取得...
//▎====================...Filter,FileList:TStrings;ContainSubDir:Boolean; lb: TLabel=nil); { 功能说明:查找一个路径下的所有文件。 参数: path:路径,filter:文件扩展名过滤,FileList:文件列表, ContainSubDir...
Delphi中遍历目录和遍历目录及子目录函数 function GetFilenames(FilePath,ExtMask: String):TStrings; //遍历目录 function GetFilenamesEx(FilePath,ExtMask :String):TStrings; //遍历目录及子目录
,然后直接使用Debug_WriteLog(),可以使用Debug_StartLog()和Debug_StopLog() 控制日志开关. * *修改记录:2011-05-15 18:15:52 * 2011-06-25 00:05:51 修改直调函数增加控制日志开关函数 2011-07-04 02:39...
0060 TStrings与TStringList的使用 37 0061 如何实现窗体文件转换 37 第3章 程序算法 39 3.1 计算类算法 40 0062 如何计算1~100的阶乘和 40 0063 如何实现最大公约数 40 0064 如何实现最小公倍数 41 ...
0060 TStrings与TStringList的使用 37 0061 如何实现窗体文件转换 37 第3章 程序算法 39 3.1 计算类算法 40 0062 如何计算1~100的阶乘和 40 0063 如何实现最大公约数 40 0064 如何实现最小公倍数 41 ...
Items: TStrings; var Processed: Boolean) of object; The event of this type was added in Grid (TDBGridEh.OnFillSTFilterListValues), in the grid column (TColumnEh.OnFillSTFilterListValues) and in ...