Delphi 中 TStrings 一些用法

來源:互聯網
上載者:User
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目錄下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.