6 REPLACE$

This routine replaces a all instances of a search string within one string with a replacement string.

A%=6:CALL&4083,string$,find$,replace$

All arguments are passed by reference and must be either movable strings or fixed strings.

The following program:

10 string$="Replace one thing with another thing."
20 find$="thing"
30 replace$="STRING"
40 A%=6:CALL&4083,string$,find$,replace$
50 PRINT string$

would output:

Replace one STRING with another STRING.

If either string$ or find$ is an empty string, the original string is left untouched. If replace$ is an empty string, all instances of find$ are removed from string$.

BASIC wrapper function

You can wrap up the routine as a standard BASIC function using the following snippet:

DEFFN_REPLACE$(s$,f$,r$)LOCALA%:A%=6:CALL&4083,s$,f$,r$:=s$

You could then invoke it like this:

PRINT FN_REPLACE$("BBC BASIC is a good programming language.", "good ", "great")

which would output

BBC BASIC is a great programming language.