type
TToken=record
strName:string;
strValue:string;
end;
const
TokenCnt = 4;
strToken:array[0..TokenCnt-1] of TToken=(
(strName:'a1';strValue:'1'),
(strName:'a11';strValue:'2'),
(strName:'a12';strValue:'3'),
(strName:'b1';strValue:'a122')
);
strOper:set of char=['+','-','*','/','(',')'];
function AStringReplace(strOld:string):string;
var
i:integer;
strValue:string;
begin
strValue:=strOld;
for i:=0 to TokenCnt-1 do
if strOld=strToken[i].strName then
strValue:=strToken[i].strValue;
Result:=strValue;
end;
function MyReplace(strTemp:string):string;
var
strVar:string;
strValue:string;
i:integer;
begin
strVar:='';
strValue:='';
for i:=1 to Length(strTemp) do
begin
if (strTemp[i] in strOper) then
begin
strVar:=AStringReplace(strVar);
strValue:=strVar+strTemp[i]+MyReplace(Copy(strTemp,i+1,length(strTemp)));
break;
end else
strVar:=strVar+strTemp[i];
end;
if strValue='' then
strValue:=AStringReplace(strTemp);
Result:=strValue;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
strTemp:string;
begin
strTemp:=Edit1.Text;
strTemp:=MyReplace(strTemp);
Label1.Caption:=strTemp;
end;
在楼上的基础上加了点
function TForm1.ChangeStr(str: string): string;
var
s: string;
splitcount: integer;
i,j: integer;
splitPosition: TStringList;
tempstr:string;
ParamSet: array of string;
splitSet: array of string;
begin
try
splitPosition:=TStringList.Create;
splitPosition.clear;
splitPosition.add('0');
s:= str;
splitCount:=0;
i:=0;j:=0;
for i:=0 to length(str)-1 do
begin
if s[i] in split_set then
begin
inc(splitcount);
splitPosition.add(inttostr(i));
end;
end;
setlength(splitSet,splitcount);
setlength(ParamSet,splitcount+1);
i:=0;
j:=0;
for i:=0 to splitPosition.count-2 do
begin
ParamSet[j]:=copy(s,strtoint(splitPosition.Strings[i])+1,strtoint(splitPosition.Strings[i+1])-strtoint(splitPosition.Strings[i])-1);
splitSet[j]:=copy(s,strtoint(splitPosition.Strings[i+1]),1);
inc(j);
end;
i:=splitPosition.count-2;
paramSet[j]:=copy(s,strtoint(splitPosition.Strings[i+1])+1,length(s)-strtoint(splitPosition.Strings[i+1]));
j:=0;
i:=0;
for j:=0 to splitcount do
begin
for i:=0 to TokenCnt-1 do
begin
if paramSet[j]=strToken[i].strName then paramSet[j]:=strToken[i].strValue;
end;
end;
i:=0;
for i:=0 to splitcount-1 do
begin
result:=Result+paramSet[i]+splitSet[i];
end;
result:=Result+paramSet[i];
finally
splitPosition.free;
splitSet:=nil;
ParamSet:=nil;
end;
end;
const
TokenCnt = 4;
strToken:array[0..TokenCnt-1] of TToken=(
(strName:'a1';strValue:'1'),
(strName:'a11';strValue:'2'),
(strName:'a12';strValue:'3'),
(strName:'b1';strValue:'a122')
);
strOper:set of char=['+','-','*','/'];
function AStringReplace(strOld:string):string;
var
i:integer;
strValue:string;
begin
strValue:='';
for i:=0 to TokenCnt-1 do
if strOld=strToken[i].strName then
strValue:=strToken[i].strValue;
Result:=strValue;
end;
function MyReplace(strTemp:string):string;
var
strVar:string;
strValue:string;
i:integer;
begin
strVar:='';
strValue:='';
for i:=1 to Length(strTemp) do
begin
if (strTemp[i] in strOper) then
begin
strVar:=AStringReplace(strVar);
strValue:=strVar+strTemp[i]+MyReplace(Copy(strTemp,i+1,length(strTemp)));
break;
end else
strVar:=strVar+strTemp[i];
end;
if strValue='' then
strValue:=AStringReplace(strTemp);
Result:=strValue;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
strTemp:string;
begin
strTemp:=Edit1.Text;
strTemp:=MyReplace(strTemp);
Label1.Caption:=strTemp;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
strTemp:string;
begin
strTemp:=Edit1.Text;
for i:=0 to TokenCnt-1 do
begin
strTemp:=StringReplace(strTemp,strToken[i].strName,strToken[i].strValue,[rfReplaceAll]);
end;
Label1.Caption:=strTemp;
end;