`

TStrings类型变量

 
阅读更多

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 = 'a=1,b=2,c=3';
var
SL:TStrings;
I:Integer;
begin
SL:= TStringList.Create;
SL.CommaText := constr;//SL内容为a=1 b=2 c=3
forI := 0 to SL.Count-1 do
begin
ShowMessage(SL.Names[I]); //分别为a,b,c
ShowMessage(SL.Values[SL.Names[I]]);//分别为1,2,3
ShowMessage(SL.ValueFromIndex[I]);//分别为1,2,3
end;
end;
这几个属性用来处理ini文件是非常好用的。

分享到:
评论

相关推荐

    自定义控件之StringList

    vs2005自定义控件,实现了delphi中TStrings(TStringList)一样的效果

    win7常用小工具打包(有快捷启动面板、CPU内存信息和天气等)

    包括CPU内存信息、MSN天气、快捷启动面板、日历、显卡温度GPUObserver32、硬盘等

    Delphi将TableToClass 表文件转换成类文件.rar

     //取得SQLServer字段的类型,返回相应Delphi变量类型  function GetColumnType(AType: integer): string;  //取得SQLServer字段的类型,返回相应字符串  function GetDBColumnType(AType: integer): string;...

    json4delphi:JSON for Delphi,支持较早版本的Delphi(6或更高版本)

    json4delphi JSON for Delphi,支持较早版本的Delphi(6或更高版本) 对象-pascal本机代码,仅使用TList,TStrings和TStringList类例子: var Json: TJson; Str: Stringbegin Json := TJson.Create(); // put Json....

    liuoffice_COM_

    sts:Tstrings):boolean;//将数据保存到excel文件中(已有文件名,保存到工作表,开始行,开始列,该列不为空跳过-1顺次保存,数据集|)function SaveDataToXls(const filename:string;sheetbrowbcolnilcol:integer;...

    DELPHI执行DOS命令控件.rar

    property OutputText:TStrings read fOutputText; property ErrorText:TStrings read fErrorText; property Running : boolean read fRunning; published property OnOutputText :TOnText read fOnOutputText ...

    delphi 字符串、数组函数

    关于 Delphi字符串、数组的函数,包含【Copy】、【Concat】等函数

    PB扩展组件

    TList 基本列表管理对象,可用于管理多种不同类型的数据或对象的对象。 TStrings 基本字符串管理对象。 TCompartStringList 是TStrings的扩充,可以指定分隔符。 TStringList 是TStrings的扩充,可以给每一个串...

    选择一个选项后可以返回一个值的ComboBox

    FValues: TStrings; FOnChange: TNotifyEvent; function GetValue: string; function GetButtonValue(Index: Integer): string; procedure SetValue(const Value: string); procedure SetValues(Value: TStrings); ...

    GetFilenames

    Delphi中遍历目录和遍历目录及子目录函数 function GetFilenames(FilePath,ExtMask: String):TStrings; //遍历目录 function GetFilenamesEx(FilePath,ExtMask :String):TStrings; //遍历目录及子目录

    详细的Delphi属性,新手

    delphi:子符串替换函数 delphi:TStrings的详细用法

    TEditingGrid控件

    property PickList: TStrings;;;; 其中EditStyle属性是控制各个单元格中按钮的类型(esSimple, esEllipsis, esPickList),PickList属性是控制当单元格为下拉框模式(esPickList)时在下拉框中出现的值列表。 这...

    DELPHI分级别调试日志类,支持多种类型输出,支持多线程模式.

    procedure DebugLog_SetStrings(LogsStrings:TStrings); procedure DebugLog_Start; procedure DebugLog_Stop; *调用方式: 如果需要设置自定义日志文件名及路径则需要先调用Debug_InitiLog() ,然后直接使用...

    报表票据打印/建立SQL查询条件/数据功能扩张

    1、打印控件: 针对票据(无),表格(TDataSet,StringGrid),文件(TStrings)而设计,可自由设置打印样式。2、报表设计器:wydReport.exe3、查询条件建立控件: 可建立含SQL条件4、LuckJump1 数据功能扩张,含显示设置、...

    delphi TStringList的使用

    TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的。 常规的用法大家都知道,现在来讨论它的一些高级的用法。

    RegMutilSz.zip

    注意Delphi版本,网络上的代码大多是Delphi7基于Ansi编码格式读取,默认的PChar是AnsiChar,不是WideChar,这样... TS: TStrings): Boolean; function WriteMultiSZ(const ValueName: string; TS: TStrings): Boolean;

    [原创] BusinessSkinForm 7.5 版本皮肤 DLL

    自己在作一些小工具的时候,对那千篇一律的窗口实在是深恶痛绝,于是将常使用的皮肤控件BusinessSkinForm中的皮肤,汇集一下,做了一个DLL,用起来也方便。...function GetSkinsName(var aStrings: TStrings): Boolean;

    Delphi网络公用函数

    //获取网络中的所有网络类型 Function NetGetNetList(var List: Tstringlist): Boolean; //获取网络中的工作组 Function NetGetGroupList(var List: TStringList): Boolean; //获取工作组中所有计算机 ...

    Delphi动态建立数据库别名.rar

    Delphi动态建立数据库别名的源码实例,源码包中一共介绍了4种在Delphi程序中动态地建立和使用别名的方法,每种方法都附有完整的源码文件,AliasN为数据库别名,DbLocat为数据库别名对应的目录,DriverN为驱动类型,对...

Global site tag (gtag.js) - Google Analytics