replace

This function searches a string for a substring and replaces all occurrences of this substring with another text.

ASSIGN result := replace (haystack; needle; new)

The result of the function replace is of type TEXT. This function has three parameters:

  1. haystack, type TEXT; the text that the substitution is performed on.
  2. needle, type TEXT; the text that is located.
  3. new, type TEXT; the replacement text.

The result of the function replace is the text from the parameter haystack where all occurrences of needle have been replaced with the parameter new.

Notes

The function replace will search for non-overlapping occurrences of the substring needle. After a match is found the string is replaced and the search continues in the remaining text. Overlapping sub-strings or matches in the content of the text new are not considered for replacement.

If the substring needle is empty no replacements will occur.

The function replace is designed to handle Unicode input. Non-printable word processor instructions are ignored, similar to the behavior of the function fragment_of_characters.

Example

replace ("The Quick Brown Fox Jumps Over The Lazy Dog"; "o"; "XoX")

Result: "The Quick BrXoXwn FXoXx Jumps Over The Lazy DXoXg".

Example

replace ("Overlapping example: XXX"; "XX"; "aX")

Result: "aXX" (the remaining "XX" is not replaced because it is composed from part of the parameter new and the original text in the parameter haystack.)