Link to home
Start Free TrialLog in
Avatar of mathes
mathes

asked on

find and replace in strings

Hi experts,

I am searching a source code which replaces all occurencies of an "old" substring in a master-string
to a "new" substring. If the "old" substring can't be found in the master-string, the routine must be
quit without run-time error.

Have you suggestions for me how this could be accomplished?

With kind regards

Mathes
ASKER CERTIFIED SOLUTION
Avatar of ZifNab
ZifNab

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kretzschmar
hi mathes,

other method

in a function

function replace_it(SourceS, OldS, NewS : String) : String;
var
  P : Integer;
  STmp, STmp2 : String;
begin
  STmp2 := '';
  STmp := SourceS;
  P := pos(OldS,STmp);
  while p <> 0 do
  begin
    STmp2 := STmp2 + copy(STmp,1,p-1)+NewS;
    delete(STmp,1,p+Length(OldS)-1);
    P := pos(OldS,STmp);
  end;
  STmp2 := STmp2 + STmp;
  result := STmp2;
end;

to call example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.text := replace_it(memo1.text,edit1.text,edit2.text);
end;

meikl
Avatar of mathes
mathes

ASKER

Hi experts,

thank you for your excellent advice.