VBScript » Functions » Replace

Syntax:
Replace(String, FindSubstring, ReplaceSubstring, Start, Count, Compare)
String
The String argument is the string to be searched.
FindSubstring
The FindSubstring argument is the substring you are searching for inside the string.
ReplaceSubstring
The ReplaceSubstring argument is the the new substring string that you wish to insert inside the string.
Start
The optional Start argument specifies the position number, counting from the left, where you wish to start the search.
Count
The optional Count argument specifies how many times to replace the substring.
Compare
The optional Compare argument must only use either a constant or value from the Comparison Constants table.

The Replace function replaces a specified substring within a specified string with a new specified substring and returns the modified string.

Comparison Constants

CONSTANT VALUE DESCRIPTION
VBBinaryCompare 0 Binary comparison
VBTextCompare 1 Text Comparison
VBDataBaseCompare 2 Compare information inside database

Examples

Code:
<% =Replace("How now brown cow?", "brown", "purple") %>
Output:
How now purple cow?
Language(s): VBScript
Code:
<% =Replace("How now brown cow?", "brown", "purple", 7) %>
Output:
w purple cow?
Explanation:

Note that the portion of the string to the left of the start position will be discarded.

Language(s): VBScript
Code:
<% =Replace("red blue red-blue redblue bluered", "blue", "purple", 1, 3) %>
Output:
red purple red-purple redpurple bluered
Language(s): VBScript
Code:
<% =Replace("red blue red-blue redblue bluered", "BLUE", "PURPLE", 1, 3, 0) %>
Output:
red blue red-blue redblue bluered
Explanation:

In the example, by using VBBinaryCompare, or 0, for the Compare argument, all upper/lower case differences are obeyed in both the search and replace.

Language(s): VBScript
Code:
<% =Replace("red blue red-blue redblue bluered", "BLUE", "PURPLE", 1, 3, 1) %>
Output:
red PURPLE red-PURPLE redPURPLE bluered
Explanation:

In the example, by using VBTextCompare, or 1, for the Compare argument, all upper/lower case differences are ignored in the search and obeyed in the replace.

Language(s): VBScript

See Also: