我写了一份代码.主要的功能是取得字符串i内的字符.
比如:
211%121%49%12%167%
取出211 121 49 12 167这5个字符串
但是这些数据都是随机产生的.不是固定的.
写了下面这份代码.但是出错.
希望高手给我解答一下 while( i < sumnum) do //循环开始
begin
if Decimal.Checked then //按钮为选中状态时
begin
txttemp:=copy(Original.Text,i,3); //取Original.Text内的i位置开始的3个字符
if pos(txttemp,'%')<>0 then //如果txttemp内含有%
begin
txttemp:=copy(txttemp,1,2); //取txttemp内的前两位
i:=i+3;
end
else
begin
i:=i+4;
end;
end;
end;
比如:
211%121%49%12%167%
取出211 121 49 12 167这5个字符串
但是这些数据都是随机产生的.不是固定的.
写了下面这份代码.但是出错.
希望高手给我解答一下 while( i < sumnum) do //循环开始
begin
if Decimal.Checked then //按钮为选中状态时
begin
txttemp:=copy(Original.Text,i,3); //取Original.Text内的i位置开始的3个字符
if pos(txttemp,'%')<>0 then //如果txttemp内含有%
begin
txttemp:=copy(txttemp,1,2); //取txttemp内的前两位
i:=i+3;
end
else
begin
i:=i+4;
end;
end;
end;
var
st:Tstringlist;
s:string;
i:integer;
begin
st:=tstringlist.Create;
st.Delimiter:='%';
st.DelimitedText:='211%121%49%12%167%';
s:='';
for i:=0 to 4 do
s:=s+st.Strings[i];
showmessage(s);
st.Free;
end;
Strs: TStringList;begin
//假定字符串s为“211%121%49%12%167%”
Strs := TStringList.Create;
Strs.Text := AnsiReplaceStr(s, '%', #10);
//Strs[0] 即211 Strs[1] 即121 ...
end;或者,照你的方法:
var
Position: Integer;
TempStr, GotStr: string;
begin
TempStr := txttemp;
while Trim(TempStr) <> '' do
begin
Position := Pos('%', TempStr);
GotStr := Copy(TempStr, 1, Position-1); //GotStr即得到的数字,如211, 121 ...
TempStr := Copy(TempStr, Position + 1, Length(TempStr) - Position);
end;
end;
begin
if Decimal.Checked then //按钮为选中状态时
begin
txttemp:=copy(Original.Text,i,3); //取Original.Text内的i位置开始的3个字符
if pos('%',txttemp) <>0 then //如果txttemp内含有%
begin
txttemp:=copy(txttemp,1,2); //取txttemp内的前两位
i:=i+3;
end
else
begin
i:=i+4;
end;
end;
end;