swap() function

The swap() function searches a string expression for the search string and then, if at the start occurrence, replaces search.str with replacement.str for the amount of occurrences specified in occurrences.

Syntax

swap(str.exp, search.str, replacement.str {,occurrences{,start}})

Synonym(s)

ereplace()
change()

Parameter(s)

str.exp   String in which the specified search.str is searched for.
search.str String to search for in str.exp.
replacement.str String to replace the specified search.str.
occurrences   Specifies the number of occurrences of search.str to be replaced by replacement.str. If not specified, defaults to 0, which replaces all found occurrences.
start Specifies which occurrence of search string the replacement begins. If not specified, defaults to 1.

Example(s)

Assigns the value "The old time" to nstring.

$options ext
nstring = swap("The new time","new","old")

This outputs "++-". The swap() function replaces one occurrence of "-" starting at the second occurrence.

print swap("---","-","+",2,1)