當前位置:成語大全網 - 新華字典 - delphi TStringList的用法

delphi TStringList的用法

dephi中沒有象VB中的split函數,但是使用TStringList類的 Delimter 和 DelimtedText 兩個屬性可以很容易的把字符串分割,然後放入 TStrings裏面.

例:

s:='aa,bb,cc,dd'

ts := TStringList.Create

ts.Delimter := ','

ts.DelimtedText := s

//這時 ts裏面就存放了 aa bb cc dd 這四個strings了. 妳只要通過 ts[0]就可以取第壹個數據

function SplitString(Source, Deli: string ): TStringList;stdcall;

var

EndOfCurrentString: byte;

StringList:TStringList;

begin

StringList:=TStringList.Create;

while Pos(Deli, Source)>0 do

begin

EndOfCurrentString := Pos(Deli, Source);

StringList.add(Copy(Source, 1, EndOfCurrentString - 1));

Source := Copy(Source, EndOfCurrentString + length(Deli), length(Source) - EndOfCurrentString);

end;

Result := StringList;

StringList.Add(source);

end;

procedure StrToStrs(str:string;strs:tstrings);

var temp:string;

i:integer;

label B,E;

begin

B:

i:= ansipos('|',str);

if i=0 THEN GOTO E;

strs.Add(ansileftstr(str,i-1));

str:=ansirightstr(str,length(str)-i);

GOTO B;

E:

end;