Categories: VBScript 教程

VBScript:Replace函数

Replace函数

Replace函数将字符串的指定部分替换为特定字符串指定次数。

语法

Replace(string,find,replacewith[,start[,count[,compare]]]) 
  • string,必需参数。从中搜索要替换的输入字符串。
  • find,一个必需参数。字符串中将被替换的部分。
  • replacewith。替换字符串,将替换为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并在浏览器中执行时,上述脚本将产生以下结果:

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
terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago