ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾VBScript£¾VBScript


objects constants operators statements functions properties methods






FUNCTION:  Replace( )

Replace(String, FindSubstring, ReplaceSubstring, Start, Count, Compare)

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

There are three mandatory arguments.

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.

Code:
<% =Replace("How now brown cow?", "brown", "purple") %>

Output:
How now purple cow?

There are three optional arguments.

Start

The optional Start argument specifies the position number, counting from the left, where you wish to start the search.

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

Code:
<% =Replace("How now brown cow?", "brown", "purple", 7) %>

Output:
w purple cow?

Count

The optional Count argument specifies how many times to replace the substring.

Code:
<% =Replace("red blue red-blue redblue bluered", "blue", "purple", 1, 3) %>

Output:
red purple red-purple redpurple bluered

Compare

The optional Compare argument must only use either the constant or value of the COMPARISON CONSTANTS.

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

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.

Code:
<% =Replace("red blue red-blue redblue bluered", "BLUE", "PURPLE", 1, 3, 0) %>

Output:
red blue red-blue redblue bluered

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.

Code:
<% =Replace("red blue red-blue redblue bluered", "BLUE", "PURPLE", 1, 3, 1) %>

Output:
red PURPLE red-PURPLE redPURPLE bluered