delphi中的字符串处理

来源:百度知道 编辑:UC知道 时间:2024/09/21 15:34:04
'Kc=6564,Dh=500,V.67,IP=59.174.85.146'这是一个字符串
我想要取出 Kc和Dh相加的结果(6564+500)
大侠帮忙!

pos('KC',UpCase('Kc=6564,Dh=500,V.67,IP=59.174.85.146'))找出 ‘KC’的位置 i,
第一个‘,’的位置j,
再用copy (str,i,j-i)函数把Kc找出来,
再用个copy把去掉前面一个逗号之后的字符串取到str2,
同样方法找Dh的值

procedure TForm1.Button4Click(Sender: TObject);
var
S:STring;
SL:TStringList;
KC,DH,I:Integer;
begin
S:='Kc=6564,Dh=500,V.67,IP=59.174.85.146';
S:=StringReplace(S,',',',',[rfReplaceAll,rfIgnoreCase]);
SL:=TStringList.Create;
try
SL.CommaText := S;
for I := 0 to SL.count-1 do
begin
if Uppercase(LeftStr(S[I],2))='KC' then KC:=Strtoint(Rightstr(S[I],Length(S[I])-
Pos('=',S[I])));
if Uppercase(LeftStr(S[I],2))='DH' then DH:=Strtoint(Rightstr(S[I],Length(S[I])-
Pos('=',S[I])));
end;
ShowMessage(Inttostr(KC*DH));
finally
SL.Free;
end;
end;

字符串拆分问题,自己查一下,好多的!

<