Skip to content

$REPLACE()

$REPLACE()

Returns a string that results from replacing or dropping a substring in the first of its arguments as specified by the patterns of its other arguments.

The format for the $REPLACE function is:

$REPLACE(expr1[,expr2[,expr3]])
  • The first expression specifies the string on which $REPLACE() operates. If the other arguments are omitted, $REPLACE() returns this expression.

  • The optional second expression specifies the substring for $REPLACE() to replace. If this argument is omitted, $REPLACE() returns the first expression without modification.

  • The optional third expression specifies the replacement for the second expression. If this argument is empty or omitted, $ZREPLACE() drops all occurrences of the substring in the second expression.

  • For a process started in UTF-8 mode, the algorithm of $REPLACE() treats the string arguments as UTF-8 encoded. With VIEW “BADCHAR” enabled, $REPLACE() produces a run-time error when it encounters a malformed character.

Examples of $REPLACE()

Example:

GTM>write $replace("alphabet","abe","ABE")
alphABEt
GTM>

$REPLACE() searches for the second expression “abe” in the first expression “alphabet”. Since “abe” exists in the first expression, it replaces the occurrence with the third expression “ABE”.