Skip to main content

Replace

Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Synopsis

Replace(string,find,replacewith[,start[,count[,compare]]])

Arguments

string String expression containing substring to replace.
find Substring being searched for. If found, it is replaced by replacewith.
replacewith Replacement substring.
start Optional — Position within string where substring search is to begin. If omitted, 1 is assumed.
count Optional — Number of substring substitutions to perform. If omitted, the default value is -1, which means make all possible substitutions. Must be used in conjunction with start.
compare Optional — Numeric value indicating the kind of comparison to use when evaluating substrings. See Description section for values. If omitted, the default value is 0, which means perform a binary comparison.

Description

The compare argument can have the following values:

Constant Value Description
vbBinaryCompare 0 Perform a case-sensitive (binary) comparison.
vbTextCompare 1 Perform a not case-sensitive textual comparison.
Note:

The not case-sensitive comparison works as expected for all Caché-supported locales.

The Replace function returns the following values:

If Replace Returns
string is zero-length A zero-length string ("").
find is zero-length A copy of string.
replacewith is zero-length A copy of string with all occurrences of find removed.
start > Len(string) A zero-length string ("").
count is 0 A copy of string.

The return value of the Replace function is a substring, with substitutions made, that begins at the position specified by start and concludes at the end of the expression string.

Examples

The following example starts at the beginning of the string and replaces all (by default), or the specified number of find substrings:

Println Replace("To ski or not to ski","ski","be")
Println Replace("To ski or not to ski","ski","be",1,2)
Println Replace("To ski or not to ski","ski","be",1,1)

The following example starts at the specified location in the string and replaces all (by default), or the specified number of find substrings:

Println Replace("To ski or not to ski","ski","be",4,2)
Println Replace("To ski or not to ski","ski","be",4,1)

Note that the returned value is not the original string with substitutions; it is a substring that starts at the point specified by the fourth (start) parameter.

The following example performs binary and textual comparisons:

' A binary comparison starting at the beginning
' of the string. Returns "XXYXXPXXY". 
Println Replace("XXpXXPXXp", "p", "Y")

' A textual comparison starting at position 3.
' Returns "YXXYXXY".
Println Replace("XXpXXPXXp", "p", "Y", 3, -1, 1)
FeedbackOpens in a new tab