Expand/Shrink

find_replace

Definition: sequence res = find_replace(object needle, sequence haystack, object replacement, integer max_replacements=0)
Description: Finds needle in haystack, and replaces all or upto max_replacements occurrences with replacement.

needle: an object to search and perhaps replace.
haystack: a sequence to be inspected.
replacement: an object to substitute for instances of needle.
max_replacements: number of items to replace, or 0 to replace all occurrences

Returns a sequence, the modified haystack.
pwa/p2js: Supported, though naturally I prefer to use substitute(), as per the examples below.
Comments: Replacements will not be made recursively on the part of haystack that was already changed.

If max is 0 or less, any occurrence of needle in haystack will be replaced by replacement.
Otherwise, only the first max_replacements occurrences are replaced.

This routine is defined in builtins\findrepl.e (an autoinclude) and is provided only for compatibility with Euphoria.
Example:
?find_replace('b',"The batty book was all but in Corba.",'c',0)
?substitute("The batty book was all but in Corba.",'b','c')
?find_replace('/',"/euphoria/demo/unix",'\\',2)
?substitute("/euphoria/demo/unix",'/','\\',2)
?find_replace("theater",{"the","theater","theif"},"theatre")
?substitute({"the","theater","theif"},"theater","theatre")
--Output (each twice):
--  "The catty cook was all cut in Corca."
--  `\euphoria\demo/unix`
--  {"the","theatre","theif"}