您的位置:首页 > 编程语言 > Delphi

Delphi 中 TStrings 一些用法

2017-03-02 16:00 447 查看
Delphi 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);

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);

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);

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);

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);

ShowMessage(strs.values[strs.Names]);

ShowMessage(strs.valueFromIndex);

end;

end;

通过这个例子不难看出:

这个时候strs中的内容是:

0=aaa

1=bbb

2=ccc

3=ddd

而Names中则是:

0

1

2

3

在values中则是:

aaa

bbb

ccc

ddd
在网上看到了.原来可以这样的:

先StringReplace用一个特殊字符替代空格,然后StringReplace回来

ss:='aa|bb c| c';

ss:= StringReplace(ss,' ','#',[rfReplaceAll]);

s:= TStringList.Create;

s.Delimiter:= '|';

s.DelimitedText:= ss;

for i:= 0 to s.Count - 1 do

begin

s[i]:= StringReplace(s[i],'#',' ',[rfReplaceAll]);

memo1.Lines.Add(s[i]);

end;

DelimitedText空格也默认为分割符的原因很简单:

Borland的程序员把这个属性对应的write方法的一行代码写错了而已,

你找到classes文件中的

procedure TStrings.SetDelimitedText(const Value: string);

var

P, P1: PChar;

S: string;

begin

BeginUpdate;

try

Clear;

P := PChar(Value);

while P^ in [#1..' '] do

{$IFDEF MSWINDOWS}

P := CharNext(P);

{$ELSE}

Inc(P);

{$ENDIF}

while P^ <> #0 do

begin

if P^ = QuoteChar then

S := AnsiExtractQuotedStr(P, QuoteChar)

else

begin

P1 := P;

// while (P^ > ' ') and (P^ <> Delimiter) do

while (P^ > '') and (P^ <> Delimiter) do

看到我修改的地方了吧,大家读读代码就知道那位写源代码的大侠本意也应该如此,他多加个空格而已,所以就变成一遇到空格切分了.对了改完后要重新编译classes.pas文件,然后把输出的Dcu放到Lib目录下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  delphi Tstrings