當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


VBScript Replace用法及代碼示例


代替

替換函數將字符串的指定部分替換為指定次數的特定字符串。

用法

Replace(string,find,replacewith[,start[,count[,compare]]]) 
  • 字符串,一個必需的參數。要搜索替換的輸入字符串。

  • 找到,一個必需的參數。將被替換的字符串部分。

  • 替換為一個必需的參數。替換字符串,將根據 find 參數替換。

  • 開始,一個可選參數。指定必須搜索和替換字符串的起始位置。默認值為 1。

  • 計數,一個可選參數。指定必須執行替換的次數。

  • 比較,一個可選參數。指定要使用的比較方法。默認值為 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 Replace Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。