Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
!Search and Replace inside a STRING
MAP
ReplaceInto PROCEDURE(*STRING Into, STRING FindTxt,STRING ReplaceTxt,BYTE ClipInto=0),LONG,PROC !Returns Count
ReplaceText PROCEDURE(STRING InText,STRING FindTxt,STRING ReplaceTxt,BYTE ClipInto=0),STRING !Returns Strng with FindTxt Replaced
END
!------------------------------------------------------------------------------------------------------
ReplaceInto PROCEDURE(*STRING Into, STRING Find,STRING Repl,BYTE ClipInto=0)!,LONG,PROC !Returns Count
X LONG,AUTO
L LONG,AUTO
szI LONG,AUTO
szF LONG,AUTO
szR LONG,AUTO
FindCnt LONG
CODE !From ABError, tweaked a LOT. Supports replace the same char e.g. 10 to 13,10
Find=lower(Find) ; L=1 ; szI=SIZE(Into) ; szF=SIZE(Find) ; szR=SIZE(Repl)
IF NOT(Find=lower(Repl) AND szF=szR) THEN
LOOP
IF ClipInto THEN
X=INSTRING(Find,CLIP(lower(Into)),1,L)
ELSE
X=INSTRING(Find,lower(Into),1,L)
END
IF ~X THEN BREAK.
Into=SUB(Into,1,X-1) & Repl & SUB(Into,X+szF,szI)
L=X+szR
FindCnt+=1
END
END
RETURN FindCnt
!------------------------------------------------------------------------------------------------------
ReplaceText PROCEDURE(STRING S, STRING Find,STRING Repl, BYTE ClipInto=0)!,STRING
S2 STRING(Size(S)*2+256),AUTO !assumes 2X bigger is reasonable
CODE
S2=S
ReplaceInto(S2,Find,Repl,ClipInto)
RETURN CLIP(S2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment