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 參考大全