for i begin if ord(str[i]) 是数字的ASCII码 str1 = str1 + '[' + str[i] + ']' else str1 = str1 + str[i] end
Function TForm1.RemoveTag(ws: WideString; Const TokenList: TStrings = Nil): WideString; Var i: Integer; sl: TStrings; s1, s2: String; Begin Result := ''; s1 := ''; s2 := '[ ]'; i := 1; sl := TStringList.Create; Try sl.Clear; ws := Trim(ws) + ' '; While i <= Length(ws) Do Begin If ws[i] = '[' Then Begin s2 := ''; Repeat s2 := s2 + ws[i]; Inc(i); Until (ws[i] = ']') Or (i = Length(ws)); s2 := s2 + ws[i]; Inc(i); End Else Begin s1 := Copy(ws, i, 1); sl.Add(s2 + '=' + s1); Inc(i); s2 := '[ ]'; End; End; s1 := ''; For i := 0 To sl.Count - 1 Do s1 := s1 + sl.ValueFromIndex[i]; If Assigned(TokenList) Then Begin TokenList.Clear; TokenList.AddStrings(sl); End; Finally sl.Free; End; Result := s1; End;
1+(2+3) 结果是 [1]+[(2]+[3)] ? 那判断一下运算符下一个字符是不是 >'0' and <'9'啊,自己判断一下不就OK了嘛,基本上这个问题已经解了啊 if pos(字符串,当前位置,1)>'0' and pos(字符串,当前位置,1)<'9' then //添加'[' 或者 ']';
//我初步试了试,好像没问题。你自己琢磨一下吧^_^ function Tmp(Str:String):String ; var i:Integer; tmpStr1,tmpStr2:String; left :Boolean; begin i := Length(Str); while i<>0 do begin if Str[i] in ['0'..'9'] then begin if not (Str[i-1] in ['0'..'9','[']) then begin tmpStr1 := Copy(Str,0,i-1); tmpStr2 := Copy(str,i,Length(Str)-i+1); Str := tmpStr1 + '[' + tmpStr2; i := i+1; end; if not (Str[i+1] in ['0'..'9',']']) then begin tmpStr1 := Copy(Str,0,i); tmpStr2 := Copy(str,i+1,Length(Str)-i); Str := tmpStr1 + ']' +tmpStr2; i := i+1; end; end; i := i-1; end; if str[Length(Str)] in ['0'..'9'] then Str := Str +']'; result := Str; end;
好像和 Kevin_Lmx(繁华阅尽) 的差不多Function change(CStr:String):String; Var TmpInt,LenInt:Integer; TmpStr:String; Numin:boolean; Begin LenInt:=Length(CStr); Numin:=False; For TmpInt:=1 to LenInt Do Begin If (CStr[TmpInt] in ['0'..'9']) Then Begin If Numin=False Then Begin Numin:=True; TmpStr:=TmpStr+'['+CStr[TmpInt]; End Else TmpStr:=TmpStr+CStr[TmpInt]; End Else Begin if Numin=True Then Begin Numin:=False; TmpStr:=TmpStr+']'+CStr[TmpInt]; End Else TmpStr:=TmpStr+CStr[TmpInt]; End; End; Result:=TmpStr; End;
嗬嗬,我也写一个,献丑了。function QuotedBracket(const AString: string): string; const cBracket = '[%s]'; var P, Start: PChar; S: string; Count: Integer; begin P := Pointer(AString); if (P = nil) then Exit; while P^ <> #0 do begin Start := P; //找到数字字符 while (P^ in ['0'..'9']) do Inc(P); Count := P - Start; if (Count <> 0) then begin SetString(S, Start, Count); S := Format(cBracket, [S]); Result := Result + S; end; //end if Start := P; //跳过非数字字符 while (not (P^ in ['0'..'9', #0])) do Inc(P); Count := P - Start; if (Count <> 0) then begin SetString(S, Start, Count); Result := Result + S; end; //end if end; //end while end;
已经经过测试了,应当可以满足你的要求。 function AliasString (value:string): string; var xLeft,xRight:Boolean ; i:integer; vCh:Char; vTmp:string; vResult:string; begin vTmp:= value; xLeft:=False; xRight:=False; for i:=1 to Length(vTmp) do begin vCh :=vTmp[i]; if (not(vCh in ['0'..'9'])) then begin if xRight then begin vResult:=vResult+']'+vCh; xRight:=False; end else begin vResult:=vResult+vCh; end; end else begin if xLeft then begin vResult:=vResult+vCh; end else if xRight then begin vResult:=vResult+vCh; xLeft:=False; xRight:=True; end else begin vResult:=vResult+'['+vCh; xLeft:=False; xRight:=True; end; end; end; if xRight then vResult:= vResult+']'; Result:= vResult; end; 测试结果: 输入:sdfa234sfrwe2342fww 返回:sdfa[234]sfrwe[2342]fww
str1:=replacestr('+','[+]',str1);自己写一个replacestr函数吧,很简单的
然后再从临时数组里输出就行了
begin
if ord(str[i]) 是数字的ASCII码
str1 = str1 + '[' + str[i] + ']'
else
str1 = str1 + str[i]
end
Var
i: Integer;
sl: TStrings;
s1, s2: String;
Begin
Result := '';
s1 := '';
s2 := '[ ]';
i := 1;
sl := TStringList.Create;
Try
sl.Clear;
ws := Trim(ws) + ' ';
While i <= Length(ws) Do
Begin
If ws[i] = '[' Then
Begin
s2 := '';
Repeat
s2 := s2 + ws[i];
Inc(i);
Until (ws[i] = ']') Or (i = Length(ws));
s2 := s2 + ws[i];
Inc(i);
End
Else
Begin
s1 := Copy(ws, i, 1);
sl.Add(s2 + '=' + s1);
Inc(i);
s2 := '[ ]';
End;
End; s1 := '';
For i := 0 To sl.Count - 1 Do
s1 := s1 + sl.ValueFromIndex[i];
If Assigned(TokenList) Then
Begin
TokenList.Clear;
TokenList.AddStrings(sl);
End;
Finally
sl.Free;
End;
Result := s1;
End;
if pos(字符串,当前位置,1)>'0' and pos(字符串,当前位置,1)<'9' then
//添加'[' 或者 ']';
function Tmp(Str:String):String ;
var
i:Integer;
tmpStr1,tmpStr2:String;
left :Boolean;
begin
i := Length(Str);
while i<>0 do
begin
if Str[i] in ['0'..'9'] then
begin
if not (Str[i-1] in ['0'..'9','[']) then
begin
tmpStr1 := Copy(Str,0,i-1);
tmpStr2 := Copy(str,i,Length(Str)-i+1);
Str := tmpStr1 + '[' + tmpStr2;
i := i+1;
end;
if not (Str[i+1] in ['0'..'9',']']) then
begin
tmpStr1 := Copy(Str,0,i);
tmpStr2 := Copy(str,i+1,Length(Str)-i);
Str := tmpStr1 + ']' +tmpStr2;
i := i+1;
end;
end;
i := i-1;
end;
if str[Length(Str)] in ['0'..'9'] then Str := Str +']'; result := Str;
end;
Var
TmpInt,LenInt:Integer;
TmpStr:String;
Numin:boolean;
Begin
LenInt:=Length(CStr);
Numin:=False;
For TmpInt:=1 to LenInt Do
Begin
If (CStr[TmpInt] in ['0'..'9']) Then
Begin
If Numin=False Then
Begin
Numin:=True;
TmpStr:=TmpStr+'['+CStr[TmpInt];
End
Else
TmpStr:=TmpStr+CStr[TmpInt];
End
Else
Begin
if Numin=True Then
Begin
Numin:=False;
TmpStr:=TmpStr+']'+CStr[TmpInt];
End
Else
TmpStr:=TmpStr+CStr[TmpInt];
End;
End;
Result:=TmpStr;
End;
const
cBracket = '[%s]';
var
P, Start: PChar;
S: string;
Count: Integer;
begin
P := Pointer(AString);
if (P = nil) then Exit; while P^ <> #0 do
begin
Start := P;
//找到数字字符
while (P^ in ['0'..'9']) do Inc(P);
Count := P - Start;
if (Count <> 0) then
begin
SetString(S, Start, Count);
S := Format(cBracket, [S]);
Result := Result + S;
end; //end if Start := P;
//跳过非数字字符
while (not (P^ in ['0'..'9', #0])) do Inc(P);
Count := P - Start;
if (Count <> 0) then
begin
SetString(S, Start, Count);
Result := Result + S;
end; //end if
end; //end while
end;
function AliasString (value:string): string;
var
xLeft,xRight:Boolean ;
i:integer;
vCh:Char;
vTmp:string;
vResult:string;
begin
vTmp:= value;
xLeft:=False;
xRight:=False;
for i:=1 to Length(vTmp) do
begin
vCh :=vTmp[i];
if (not(vCh in ['0'..'9'])) then
begin
if xRight
then begin
vResult:=vResult+']'+vCh;
xRight:=False;
end
else begin
vResult:=vResult+vCh;
end;
end
else begin
if xLeft
then begin
vResult:=vResult+vCh;
end
else if xRight
then begin
vResult:=vResult+vCh;
xLeft:=False;
xRight:=True;
end
else begin
vResult:=vResult+'['+vCh;
xLeft:=False;
xRight:=True;
end;
end;
end;
if xRight
then vResult:= vResult+']';
Result:= vResult;
end;
测试结果:
输入:sdfa234sfrwe2342fww
返回:sdfa[234]sfrwe[2342]fww