` Printed Icetips Article

Icetips Article



Functions: Replace function
2004-06-04 -- Jonathan Kay
 
Newsgroups: comp.lang.clarion

> I need a procedure something like this
> ReplaceString(BigTextString,FindString,ReplaceString), where it looks for
> the FindString and replaces it with ReplaceString in the BigTextString.

I'd be inclined to do away with the 'BigString'; there may be times you
don't know how big to make it. Here's what I use - just prototype to return
a STRING:

Replace PROCEDURE(STRING Text,STRING Find,STRING Replace,BYTE CS=FALSE)
Pos  UNSIGNED,AUTO
RVal ANY ! Do not add ,AUTO
  CODE
    LOOP
      Pos =
CHOOSE(~CS,INSTRING(UPPER(Find),UPPER(Text),1),INSTRING(Find,Text,1))
      RVal = RVal & CHOOSE(~Pos,Text,Text[1 : Pos-1] & Replace)
      Text = CHOOSE(~Pos,'',Text[Pos+LEN(Find) : LEN(Text)])
    UNTIL ~Text
    RETURN RVal

Jon.



Printed November 21, 2024, 11:26 am
This article has been viewed/printed 35387 times.
Google search has resulted in 12 hits on this article since January 25, 2004.