Skip to content

Replaces

See Also: String Functions, Replace

Purpose

Replaces returns a string with every occurrence of a specified substring in a host string replaced with a new substring.

Return Type

String

Syntax

(Replaces({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

Replaces finds every occurrence of {old-substring} in the {host-string} and replaces them with the value of {new-substring}.

Procedure Test
    String sSentence
    Move "I have three eyes, three ears, three arms and three legs." to sSentence
    Showln (Replaces("three", sSentence, "two"))
End_Procedure

In this example, the value of the variable sSentence will be changed from "I have three eyes, three ears, three arms and three legs." to "I have two eyes, two ears, two arms and two legs."

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

Notes

  • Replaces 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.
  • Iterations are not supported. For example, replacing two spaces (" ") in a string with one will work, but occurrences of two spaces in the resulting string will not be replaced again.

For example:

Move "This is    a nice example" To sData // Contains three spaces
Move (Replaces("  ", sData, " ")) To sData // Contains two spaces
Showln (Pos('  ', sData)) // Shows the value of 8 which is the position of a double space

Can be solved by:

Move "This is    a nice example" To sData // Contains three spaces
While (Pos('  ', sData) > 0) // Test for two spaces
    Move (Replaces("  ", sData, " ")) To sData // Contains two spaces on first round and 1 on second
Loop
Showln (Pos('  ', sData)) // Shows the value of 0 which indicates not found