1. 程式人生 > >Delphi 中 TStrings 一些用法

Delphi 中 TStrings 一些用法

作用 edt .com 找到 min 它的 nes 還要 windows

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 一些用法