# replace

Definition: | sequence res = replace(sequence src, object replacement, integer start, integer stop=start)
Description: | Replace a slice in a sequence. src: the sequence in which replacement will be done. replacement: the item to replace the slice with. start: the start index of the slice to replace. stop: the end index of the slice to replace. Returns a sequence, with src[start..stop] replaced by replacement.
pwa/p2js: | Supported, although I strongly prefer the equivalent longhand code, which is faster anyway. There is also a JavaScript string.replace(re,s) function, which is not further documented here.
Comments: | This routine, defined in builtins\pseqc.e (an autoinclude), is simply `src[start..stop] = replacement; return src` plus a bit of extra code for when replacement is an atom, and is provided only for compatibility with Euphoria. While a different res and src may occasionally allow two steps to be performed in one line of code, when they are the same variable the use of this routine is derisively sneered at by the author of Phix - `res = replace(res,x,i,j)` is more typing, less readable, more error-prone, and slower than `res[i..j] = x`.
Example: |
    s = replace("John Middle Doe", "Smith", 6, 11)          -- s is "John Smith Doe"
    s = replace({45.3, "John", 5, {10, 20}}, 25, 2, 3)      -- s is {45.3, 25, {10, 20}}