Skip to content

Replace

See Also: String Functions, Replaces

Purpose

Replace returns a string with the first occurrence of a specified substring in a host string replaced with a new substring.

Return Type

String

Syntax

(Replace({old-substring}, {host-string}, {new-substring}))

Where:

  • {old-substring}: The substring of {host-string} to replace.
  • {host-string}: The string value to make the replacement in.
  • {new-substring}: The string value to replace {old-substring} with.

What It Does

Replace finds the first occurrence of {old-substring} in the {host-string} and replaces it with the value of {new-substring}.

Procedure Test
    String sSentence
    Move "We will be in town for two weeks." To sSentence
    Showln (Replace("two weeks", sSentence, "a fortnight"))
End_Procedure

In this example, the value of the variable sSentence will be changed from "We will be in town for two weeks." to "We will be in town for a fortnight."

The first occurrence of a substring may be removed by replacing it with an empty string "".

Notes

  • Replace is case-sensitive.
  • When {old-substring} is not found in {host-string}, the value of {host-string} is returned unchanged.
  • If {host-string} is of a type other than string, its value will be converted to a string for output.