Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

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”

<%

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

从第 1 个位置开始,将前 2 个出现的字母“i”替换为“##”

<%

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 参考大全
×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.