VBScript Replace function
Replace function to replace string with another string specified portion of the specified number of times.
grammar
Replace(string,find,replacewith[,start[,count[,compare]]])
参数 | 描述 |
---|---|
string | 必需。被搜索的字符串。 |
find | 必需。将被替换的字符串部分。 |
replacewith | 必需。用于替换的子字符串。 |
start | 可选。指定的开始位置。默认值是 1。起始位置之前的所有字符将被删除。 |
count | 可选。规定要执行的替换的次数。 默认值是 -1,表示进行所有可能的替换。 |
compare | 可选。规定要使用的字符串比较类型。默认是 0。 可采用下列的值:
|
Examples
Example 1
The word "beautiful" replaced with "fantastic":
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","fantastic"))
</script>
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","fantastic"))
</script>
Examples of the above output:
This is a fantastic day!
try it"
Example 2
The letter "i" is replaced with "##":
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##"))
</script>
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##"))
</script>
Examples of the above output:
Th##s ##s a beaut##ful day!
try it"
Example 3
The letter "i" is replaced with "##", start from position 15:
Please note that all characters are 15 positions before being deleted.
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##",15))
</script>
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##",15))
</script>
Examples of the above output:
t##ful day!
try it"
Example 4
Starting at position 1, the former two-letter "i" is replaced with "##":
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##",1,2))
</script>
txt="This is a beautiful day!"
document.write(Replace(txt,"i","##",1,2))
</script>
Examples of the above output:
Th##s ##s a beautiful day!
try it"
Example 5
The letter "t" is replaced with "##", using both text and binary comparison:
<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>
txt="This is a beautiful day!"
document.write(Replace(txt,"t","##",1,-1,1) & "<br />")
document.write(Replace(txt,"t","##",1,-1,0))
</script>
Examples of the above output:
##his is a beau##iful day!
This is a beau##iful day!
This is a beau##iful day!
try it"
Complete VBScript Reference