- VBScript 教程
- VBScript - 主页
- VBScript - 概览
- VBScript - 语法
- VBScript - 启用
- VBScript - 放置
- VBScript - 变量
- VBScript - 常量
- VBScript - 运算符
- VBScript - 决策
- VBScript - 循环
- VBScript - 事件
- VBScript - Cookie
- VBScript - 数字
- VBScript - 字符串
- VBScript - 数组
- VBScript - 日期
- VBScript 高级
- VBScript - 过程
- VBScript - 对话框
- VBScript - 面向对象
- VBScript - 正则表达式
- VBScript - 错误处理
- VBScript - 杂项语句
- VBScript 实用资源
- VBScript - 问与答
- VBScript - 快速指南
- VBScript - 实用资源
- VBScript - 讨论
VBScript Replace 函数
Replace
Replace 函数用特定的字符串替换字符串的指定部分,指定的次数。
语法
Replace(string,find,replacewith[,start[,count[,compare]]])
string,一个必需的参数。要从中搜索并替换的输入字符串。
find,一个必需的参数。将被替换的字符串部分。
replace with,一个必需的参数。替换字符串,将替换 find 参数。
start,一个可选参数。指定要从中搜索并替换字符串的起始位置。默认值为 1。
count,一个可选参数。指定要执行替换的次数。
compare,一个可选参数。指定要使用的比较方法。默认值为 0。
0 = vbBinaryCompare - 执行二进制比较
1 = vbTextCompare - 执行文本比较
示例
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> var = "This is VBScript Programming" 'VBScript to be replaced by MS VBScript document.write("Line 1: " & Replace(var,"VBScript","MS VBScript") & "<br />") 'VB to be replaced by vb document.write("Line 2: " & Replace(var,"VB","vb") & "<br />") ''is' replaced by ## document.write("Line 3: " & Replace(var,"is","##") & "<br />") ''is' replaced by ## ignores the characters before the first occurence document.write("Line 4: " & Replace(var,"is","##",5) & "<br />") ''s' is replaced by ## for the next 2 occurences. document.write("Line 5: " & Replace(var,"s","##",1,2) & "<br />") ''r' is replaced by ## for all occurences textual comparison. document.write("Line 6: " & Replace(var,"r","##",1,-1,1) & "<br />") ''t' is replaced by ## for all occurences Binary comparison document.write("Line 7: " & Replace(var,"t","##",1,-1,0) & "<br />") </script> </body> </html>
当您将它保存为 .html 并使用 Internet Explorer 执行它时,上面的脚本将产生以下结果 -
Line 1: This is MS VBScript Programming Line 2: This is vbScript Programming Line 3: Th## ## VBScript Programming Line 4: ## VBScript Programming Line 5: Thi## i## VBScript Programming Line 6: This is VBSc##ipt P##og##amming Line 7: This is VBScrip## Programming
vbscript_strings.htm
广告