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
廣告
© . All rights reserved.