Change Function

Replaces one or more instances of a substring.

Syntax

Change (string, substring, replacement [,number [,start]])

string is the string or expression in which you want to change substrings. If string evaluates to a null value, null is returned.

substring is the substring you want to replace. If it is empty, the value of string is returned (this is the only difference between Change and Ereplace).

replacement is the replacement substring. If replacement is an empty string, all occurrences of substring are removed.

number specifies the number of instances of substring to replace. To change all instances, use a value less than 1.

start specifies the first instance to replace. A value less than 1 defaults to 1.

Remarks

A null value for string returns a null value. If you use a null value for any other variable, a runtime error occurs.

Examples

The following example replaces all occurrences of one substring with another:

MyString = "AABBCCBBDDBB"NewString = Change(MyString, "BB",
→ "xxx") * The result is "AAxxxCCxxxDDxxx"

The following example replaces only the first two occurrences.

MyString = "AABBCCBBDDBB"NewString = Change (MyString, "BB",
→ "xxx", 2, 1)* The result is "AAxxxCCxxxDDBB"

The following example removes all occurrences of the substring:

MyString = "AABBCCBBDDBB"NewString = Change (MyString, "BB",
→ "")* The result is "AACCDD"