function StrReplace(Strng, Old, New: string; Anzahl: Word;
IgnoreCase: boolean): string;
var hlp, such: string;
p, lg, x: integer;
begin
result := strng;
lg := length(old);
if Ignorecase then begin
hlp := ansilowercase(strng);
such := ansilowercase(old);
end else begin
hlp := strng;
such := old;
end;
for x := 0 to anzahl - 1 do begin
p := pos(such, hlp);
if p = 0 then break
else begin
delete(hlp, p, lg);
delete(result, p, lg);
insert(new, hlp, p);
insert(new, result, p);
end;
end;
end;
function StringReplaceBack(s, such, ersatz: string; Anzahl: Word;
IgnoreCase: boolean): string;
var
lg, x: integer;
newstr, posstr: string;
begin
if anzahl > 0 then begin
if IgnoreCase then begin
newstr := AnsiLowerCase(s);
such := AnsiLowerCase(such);
end else newstr := s;
lg := length(such);
posstr := '';
for x := length(s) downto 1 do begin
posstr := newstr[x] + posstr;
if ansipos(such, posstr) > 0 then begin
delete(s, x, lg);
delete(posstr, 1, lg);
insert(ersatz, s, x);
dec(anzahl);
if anzahl = 0 then break;
end;
end;
end;
result := s;
end;
// Beispielaufrufe
procedure TForm1.Button5Click(Sender: TObject);
begin
showmessage(stringreplaceback('Es geht um den Penemekenel', 'E', 'a', 5,
true));
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
showmessage(StrReplace('Panamakanal', 'a', 'ie', 3, false));
end;