#20 function ReplaceStringAllW 'OldString not replaced by Newstr

open
nobody
Strings (5)
5
2013-01-15
2005-06-18
No

Replacements is the 'out' variable and is not
initialised to be used in the for loop...

Remove the lines commented out below to make it work.

see note *** in code below

Unit StStr.pas (SysTools for Kylix) +/- line 8604

function ReplaceStringAllW(const S, OldString,
NewString : WideString;
var Replacements : Cardinal)
: WideString;
var
I,
C : Cardinal;
P1 : longint;
Tmp: WideString;
begin
Result := S;
if (S = '') or (OldString = '') or (Pos(OldString, S)
= 0) then
Replacements := 0
else begin
Tmp := S;
P1 := AnsiPos(OldString, S);
if (P1 > 0) then begin
Result := Copy(Tmp, 1, P1-1);
C := 0;
while (P1 > 0) do begin
//for I := 1 to Replacements do begin <--- ***
Inc(C);
Result := Result + NewString;
//end;
Tmp := Copy(Tmp, P1+Length(OldString), MaxLongInt);
P1 := AnsiPos(OldString, Tmp);
if (P1 > 0) then begin
Result := Result + Copy(Tmp, 1, P1-1);
end else
Result := Result + Tmp;
end;
Replacements := C;
end else begin
Result := S;
Replacements := 0;
end;
end;
end;

Discussion