VBScript Replace 函數
完整的 VBScript 參考手冊
Replace 函數使用另一個字符串替換字符串的指定部分指定的次數。
語法
Replace(string,find,replacewith[,start[,count[,compare]]])
參數 | 描述 |
string | 必需。被搜索的字符串。 |
find | 必需。將被替換的字符串部分。 |
replacewith | 必需。用于替換的子字符串。 |
start | 可選。指定的開始位置。默認值是 1。起始位置之前的所有字符將被刪除。 |
count | 可選。規定要執行的替換的次數。 默認值是 -1,表示進行所有可能的替換。 |
compare | 可選。規定要使用的字符串比較類型。默認是 0。 可采用下列的值: - 0 = vbBinaryCompare - 執行二進制比較
- 1 = vbTextCompare - 執行文本比較
|
實例
實例 1
把單詞 "beautiful" 替換為 "fantastic":
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","fantastic"))
</script>
以上實例輸出結果:
This is a fantastic day!
嘗試一下 ? 實例 2
把字母 "i" 替換為 "##":
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##"))
</script>
以上實例輸出結果:
Th##s ##s a beaut##ful day!
嘗試一下 ? 實例 3
把字母 "i" 替換為 "##",從位置 15 開始:
請注意,位置 15 之前的所有字符都會被刪除。
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##",15))
</script>
以上實例輸出結果:
t##ful day!
嘗試一下 ? 實例 4
從位置 1 開始,把前 2 個字母 "i" 替換為 "##":
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##",1,2))
</script>
以上實例輸出結果:
Th##s ##s a beautiful day!
嘗試一下 ? 實例 5
把字母 "t" 替換為 "##",采用文本和二進制比較:
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"t","##",1,-1,1) & "<br />")
document.write(Replace(txt,"t","##",1,-1,0))
</script>
以上實例輸出結果:
##his is a beau##iful day!
This is a beau##iful day!
嘗試一下 ?
完整的 VBScript 參考手冊