时间:-5-26来源:yang 作者: peng点击: 49次
第一种方法, 写一个自定义函数
Function Spilter( S:String; Var Ans:TStrings):Boolean;
Var i:Integer;
St:String;
Begin
Try
result:=True;
if Ans=nil then Ans:=TStringList.Create;
Ans.Clear;
St:=‘‘;
For i:=1 to Length(S) do
if S[i]=‘,‘ then Begin
Ans.Append(St);
St:=‘‘;
end else Begin
St:=St+S[i];
end;
if St<>‘‘ then Ans.Append(St);
except
result:=False;
end;
end;
第二种方法, 使用maText属性
maText := S;
实质上是调用了SetDelimitedText函数:
procedure TStrings.SetDelimitedText(const Value: string);
var
P, P1: PChar;
S: string;
begin
Clear;
P := PChar(Value);
while P^ in [#1..‘ ‘] do //过滤掉从#1(SOH) 到 #32(Space) 之间的控制字符和space
P := CharNext(P); //移向下一个字符
while P^ <> #0 do
begin
if P^ = QuoteChar then //过滤掉双引号(QuoteChar)
S := AnsiExtractQuotedStr(P, QuoteChar)
else
begin
P1 := P; //P1为当前字符串的快照
while (P^ > ‘ ‘) and (P^ <> Delimiter) do
P := CharNext(P); //P向后移动,直到遇见分隔符(逗号)
SetString(S, P1, P - P1); //将这一趟的结果复制到S, P-P1是两个指针(地址)的差
end;
Add(S);
while P^ in [#1..‘ ‘] do
P := CharNext(P);
if P^ = Delimiter then
begin
P1 := P;
if CharNext(P1)^ = #0 then
Listbox1.Add(‘‘);
repeat
P := CharNext(P);
until not (P^ in [#1..‘ ‘]);
end;
end;
end;