Below is the code w/ the fixes I made.
TmpStr := S;
C := 0;
I := Pos(AString, TmpStr);
while (I > 0) do begin
// Inc(C, I + Length(AString));
Inc(C, I + Length(AString)-1); // <------- fixed by edb
// System.Delete(TmpStr, 1, I + Length(AString));
System.Delete(TmpStr, 1, I + Length(AString)-1); // <----- fixed by edb
I := Pos(AString, TmpStr);
end;
{Go back the length of AString since the while loop deletes the last
instance}
// Dec(C, Length(AString));
Dec(C, Length(AString)-1); // <----- fixed by edb
Position := C;
Result := True;
The problem surfaced when I was looking for a string of length 1. The
original code may have worked fine for strings longer than 1 character, but
I didn't test that.
Eric