在csh中,如果从一行字符串中提取出需要的多个字段?比如

来源:百度知道 编辑:UC知道 时间:2024/05/28 09:29:06
从abc123:abc123,xyz456 789,AB12pp yy
中,顺次提取出abc123
xyz456 789
AB12pp yy
这三个字符串?

建立一个这样的函数
function SplitString(const Source,ch:String):TStringList;
var
temp:String;
i:Integer;
begin
Result:= TStringList.Create;
//如果是空自符串则返回空列表
if Source='' then exit;
temp:=Source;
i:=pos(ch,Source);
while i<>0 do
begin
Result.add(copy(temp,0,i-1));
Delete(temp,1,i);
i:=pos(ch,temp);
end;
Result.add(temp);
end;

然后建立一个TstringList;
var ss:TstringList;

然后引用即可:
ss:=SplitString(abc123:abc123,xyz456 789,AB12pp yy ',',');
其后用ss[0],ss[1],ss[2]来访问。
这是一个通用的方法,以后可以用任意的分隔符来分隔。

建立一个这样的函数
function SplitString(const Source,ch:String):TStringList;
var
temp:String;
i:Integer;
begin
Result:= TStringList.Create;
//如果是空自符串则返回空列表
if Source='' then exit;
temp:=Source;
i:=pos(ch,Source);
while i<>0 do
begin
Result.add(copy(tem