[comp.lang.pascal] Substitution function

fehr@ms.uky.edu (Jeff Davis) (04/25/91)

 I saw a request for a substitution routine. This is a generalized routine
that I use all the time. I'm sure there are better algorithms out there,
but this seems to work. Overflows are simply truncated by Turbo.


function simp(needle,haystack,sub:string):string;
var
   i,j   : integer;
   a,b   : string;

   begin
        simp:=haystack;
        if pos(needle,haystack)=0 then exit;
        j:=length(needle);
        b:='';
        i:=1;
        while i <= length(haystack) do
        begin
             a:=copy(haystack,i,j);
             if a=needle then
             begin
                  b:=b+sub;
                  inc(i,j);
             end
             else
             begin
                 b:=b+haystack[i];
                 inc(i,1);
             end;
        end;
        simp:=b;
   end;

-- 
davis@keats.ca.uky.edu
Is this a long trip or a short trip?