VBScript Replace 函数
❮ VBScript 参考大全
Replace 函数可以根据指定次数将字符串中指定的某部分替换为另一个字符串。
语法
Replace(string,find,replacewith[,start[,count[,compare]]])
参数 | 描述 |
---|---|
string | 必需。要搜索的字符串 |
查找值 | 必需。将被替换的字符串的一部分 |
replacewith | 必需。替换子字符串 |
start | 可选。指定开始位置。默认为 1。开始位置之前的字符都将被删除。 |
count | 可选。指定要执行的替换次数。 默认值为 -1,表示执行所有可能的替换 |
比较 | 可选。指定要使用的字符串比较。默认值为 0 可取以下值之一
|
示例
示例 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 参考大全