procedure SeparateTerms(s : string;Separator : char;Terms : TStringList); var hs : string; p : integer;begin Terms.Clear; // 清除所有的terms if Length(s)=0 then // 如没有任何分隔字符存在就退出 Exit; p:=Pos(Separator,s); while P<>0 do begin hs:=Copy(s,1,p-1); // 复制 term Terms.Add(hs); // 加入 term Delete(s,1,p); // 清除分隔符 p:=Pos(Separator,s); // 寻找下一个分隔符 end; if Length(s)>0 then Terms.Add(s); // 结尾剩余字符加入 term end;
用POS来定位, 用COPY来得到结果
如果是固定长度很好办,按长度读就是。 要不然就用POS,得到位置,再copy就行了。
或用LEFTSTR 和RIGHTSTR函数 lefrstr(你的字符串,位数)
var a:array[1..n]of string; s:STRING: i,k:integer; begin k:=1; a[1]:=''; for i:=1 to length(s) do begin if s=';' then begin k:=k+1; a[k]:='';end else a[k]:=a[k]+s[i]; end; end;
也就是字符串处理函数
一个一个的读取字符碰到是“;”的时候,截断然后把截断的字符串放到数组中第二种方法:
使用Pos函数,获得“;”的位置,然后使用Copy的方式获得字符串,放到数组中具体的函数原型请参见Delphi的帮助
var
hs : string;
p : integer;begin
Terms.Clear; // 清除所有的terms
if Length(s)=0 then // 如没有任何分隔字符存在就退出
Exit;
p:=Pos(Separator,s);
while P<>0 do
begin
hs:=Copy(s,1,p-1); // 复制 term
Terms.Add(hs); // 加入 term
Delete(s,1,p); // 清除分隔符
p:=Pos(Separator,s); // 寻找下一个分隔符
end;
if Length(s)>0 then
Terms.Add(s); // 结尾剩余字符加入 term
end;
用COPY来得到结果
要不然就用POS,得到位置,再copy就行了。
lefrstr(你的字符串,位数)
s:STRING:
i,k:integer;
begin
k:=1;
a[1]:='';
for i:=1 to length(s) do
begin
if s=';' then begin k:=k+1; a[k]:='';end
else a[k]:=a[k]+s[i];
end;
end;