VBScript Replace 函数


❮ 完整的 VBScript 参考

Replace 函数可使用一个字符串替换另一个字符串指定的次数。

语法

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

参数 描述
string 必需的。需要被搜索的字符串。
find 必需的。将被替换的字符串部分。
replacewith 必需的。用于替换的子字符串。
start 可选。规定开始位置。默认是 1。
count 可选。规定指定替换的次数。默认是 -1,表示进行所有可能的替换。
compare 可选。规定所使用的字符串比较类型。默认是 0。

Can 具有以下值之一:

  • 0 = vbBinaryCompare - 执行二进制比较。
  • 1 = 执行文本比较。

Replace 可能返回的值:

参数可能的取值 Replace 返回的值
expression 为零长度 零长度字符串 ("")。
expression 为 Null 错误。
find 参数为零长度 expression 的副本。
replacewith 参数为零长度 expression 的副本,其中删除了所有由 find 参数指定的内容。
start > Len(expression) 零长度字符串。
count 为 0 expression 的副本。

实例

实例 1

将"beautiful"一词替换为"fantastic":

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))

%>

上述代码的输出为:

This is a fantastic day!
显示示例 »

实例 2

将字母"i"替换为"##":

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))

%>

上述代码的输出为:

Th##s ##s a beaut##ful day!
显示示例 »

实例 3

将字母"i"替换为"##",从位置 15 开始:

请注意,位置 15 之前的所有字符都将被删除。

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))

%>

上述代码的输出为:

t##ful day!
显示示例 »

实例 4

将字母"i"的第一次出现的 2 次替换为"##",从位置 1 开始:

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))

%>

上述代码的输出为:

Th##s ##s a beautiful day!
显示示例 »

实例 5

用"##"替换字母"t",用文本和二进制比较:

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))

%>

上述代码的输出为:

##his is a beau##iful day!
This is a beau##iful day!
显示示例 »

❮ 完整的 VBScript 参考