VBScript Replace 函數
定義和用法
Replace 函數可使用一個字符串替換另一個字符串指定的次數。
語法
Replace(string,find,replacewith[,start[,count[,compare]]])
參數 |
描述 |
string |
必需的。需要被搜索的字符串。 |
find |
必需的。將被替換的字符串部分。 |
replacewith |
必需的。用于替換的子字符串。 |
start |
可選的。規定開始位置。默認是 1。 |
count |
可選的。規定指定替換的次數。默認是 -1,表示進行所有可能的替換。 |
compare |
可選的。規定所使用的字符串比較類型。默認是 0。 |
參數 compare 的值:
常數 |
值 |
描述 |
vbBinaryCompare |
0 |
執行二進制比較。 |
vbTextCompare |
1 |
執行文本比較。 |
Replace 可能返回的值:
參數可能的取值 |
Replace 返回的值 |
expression 為零長度 |
零長度字符串 ("")。 |
expression 為 Null |
錯誤。 |
find 參數為零長度 |
expression 的副本。 |
replacewith 參數為零長度 |
expression 的副本,其中刪除了所有由 find 參數指定的內容。 |
start > Len(expression) |
零長度字符串。 |
count 為 0 |
expression 的副本。 |
實例
例子 1
dim txt
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","horrible"))
輸出:
This is a horrible day!