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:

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 "" (empty string).

Notes

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