VBScript Replace 函数

定义和用法

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

语法

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

参数 compare 的值:

常数 描述
vbBinaryCompare 0 执行二进制比较。
vbTextCompare 1 执行文本比较。

Replace 可能返回的值:

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

实例

例子 1

dim txt
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","horrible"))

输出:

This is a horrible day!