Re: Ascii / Text Replacement / Substitution ?
Posted in 1997
John, Here's a function that I've used to do what your looking for. {############################################################################## # Function : str_stuff # Programmer : Joseph Cullipher # Date : 03/05/96 # Description : This function replaces ALL occurences of a string with # : another string inside of a third string # # Received Parameters: Mfind (the string you want to replace) # : Mreplace (the string you want to replace with) # : Mtarget (the string to do it with) # # Returned Parameters: Mstring2 (the result of this function) # ###############################################################################} function str_stuff(Mfind,Mreplace,Mtarget) define Mfind,Mreplace,Mtarget,Mstring,Mspaces,Mstring2 char(512), Mi,Mk,Mlen_find,Mlen_repl,Mlen_targ,Msave smallint, Mtmpstr1,Mtmpstr2 char(512) let int_flag=false let Mlen_find=length(Mfind) let Mlen_repl=length(Mreplace) let Mlen_targ=length(Mtarget) let Mspaces=(512 spaces) let Msave=1 for Mi=1 to Mlen_targ-Mlen_find+1 let Mk=Mi+Mlen_find-1 if Mk<Mi then let Mk=Mi end if if Mfind clipped=Mtarget[Mi,Mk] then case when Mi=1 let Mstring=Mreplace clipped let Mtarget=Mspaces[1,Mk],Mtarget[(Mk+1),Mlen_targ] if Mlen_repl<Mlen_find then let Msave=Mk else let Msave=Mk+1 end if while Mreplace[1,Mlen_find]=Mtarget[Msave,(Msave+Mlen_find-1)] while Mstring[1,1]=" " let Mstring=Mstring[2,512] clipped end while let Mstring2=Mstring2 clipped,Mstring clipped let Msave=Msave+Mlen_find let Mstring=Mreplace clipped let Mtarget=Mspaces[1,Msave-1],Mtarget[(Msave),Mlen_targ+1] end while when Mk=Mlen_targ let Mstring=Mtarget[Msave,(Mi-1)],Mreplace clipped let Mtarget="" otherwise let Mstring=Mtarget[Msave,Mi-1],Mreplace clipped let Mtarget=Mspaces[1,Mk],Mtarget[(Mk+1),Mlen_targ] if Mlen_repl<Mlen_find then let Msave=Mk else let Msave=Mk+1 end if while Mreplace[1,Mlen_find]=Mtarget[Msave,(Msave+Mlen_find-1)] while Mstring[1,1]=" " let Mstring=Mstring[2,512] clipped end while let Mstring2=Mstring2 clipped,Mstring clipped let Msave=Msave+Mlen_find let Mstring=Mreplace clipped let Mtarget=Mspaces[1,Msave-1],Mtarget[(Msave),Mlen_targ+1] end while end case while Mstring[1,1]=" " let Mstring=Mstring[2,512] clipped end while let Mstring2=Mstring2 clipped,Mstring clipped end if end for if length(Mtarget)>0 then while Mtarget[Msave,Msave]=" " let Msave=Msave+1 end while let Mstring2=Mstring2 clipped,Mtarget[Msave,Mlen_targ] end if return Mstring2 clipped end function HTH Joe ------------------------------------------------------------------------- Joseph Cullipher <joseph@cannonexpress.com> Standard Disclaimers Apply ------------------------------------------------------------------------- On Mon, 24 Nov 1997, John Cokos wrote: > This one should be easy for you pros: > > In 4gl, I just need to replace (Like sed) characters within a string. > Specifically, I need to replace a certain 4 characters with another... > > ie. cData = "Freds Mom is dumb, really dumb" > How do I change cData to "Freds Mom is cool, really cool" > > Or, for a tougher example (The one i'm working on for a CGI project): > cData = "This is line 1\\r\\nThis is line 2\\r\\n\\r\\nThis is line 4" > I need to change that into: > "This is line 1<BR>This is line 2<BR><BR>This is line 4" > > I can insert the "<BR>" into the string fine, but I cant get rid of the > \\r or the \\n > > If possible, Id like to do this in 4gl, not with c > > Thanks in advance >