Replace substring to any string

Posted in Algorithms

Use Replace function to replace SubStr string to Str string in Dest string. In other words, if you have Dest is '1234567890' and SubStr is '345' and Str is 'ABCDE', then you will receive Dest as '12ABCDE67890' This function use Pos function, Delete and Insert procedures.

function Replace(Dest, SubStr, Str: string): string;
var
  Position: Integer;
begin
  Position:=Pos(SubStr, Dest);
  Delete(Dest, Position, Length(SubStr));
  Insert(Str, Dest, Position);
  Result:=Dest;
end;