Skip to main content

EREPLACE

Replaces a substring in a string.

Synopsis

EREPLACE(string,substring,replacement[,occurrence[,begin]])

Arguments

string An expression that resolves to a string.
substring An expression that resolves to a substring found within string.
replacement An expression that resolves to the substring used to replace substring.
occurrence Optional — An expression that resolves to an integer count specifying how many occurrences of substring to replace. The default is to replace all occurrences. A value of 0 replaces all occurrences. When using begin, you must specify an occurrence value.
begin Optional — An integer count specifying the instance of substring with which to begin replacement. The default is to begin with the first instance of substring.

Description

The EREPLACE function replaces each occurrence of substring in a string with a new value. Whether to replace all instances of substring is specified by the optional occurrence and begin arguments. If these are omitted, all occurrences of substring are replaced by replacement. The replacement string can be longer or shorter than the substring it replaces.

If substring is not found in string, EREPLACE returns string unchanged. If substring is the empty string ("") the replacement string is appended to the beginning of string.

If replacement is the empty string (""), EREPLACE removes instances of substring from string.

Examples

The following example uses the EREPLACE function to replace all instances of a substring:

x="The slow brown fox slowly leapt"
PRINT EREPLACE(x,"slow","quick")
   ! Returns "The quick brown fox quickly leapt"

The following example also replaces the specified instances of a substring:

x="The slow brown fox slowly leapt"
PRINT EREPLACE(x,"slow","quick",1)
   ! Returns "The quick brown fox slowly leapt"
PRINT EREPLACE(x,"slow","quick",0,2)
   ! Returns "The slow brown fox quickly leapt"

The following example appends the replacement value to the string:

x="there was a slow brown fox"
PRINT EREPLACE(x,"","Once upon a time ")
   ! Returns "Once upon a time there was a slow brown fox" 

See Also

FeedbackOpens in a new tab