ฟังก์ชันแทนที่ VBScript
❮ การอ้างอิง VBScript ที่สมบูรณ์
ฟังก์ชันแทนที่แทนที่ส่วนที่ระบุของสตริงด้วยสตริงอื่นตามจำนวนที่ระบุ
ไวยากรณ์
Replace(string,find,replacewith[,start[,count[,compare]]])
Parameter | Description |
---|---|
string | Required. The string to be searched |
find | Required. The part of the string that will be replaced |
replacewith | Required. The replacement substring |
start | Optional. Specifies the start position. Default is 1. All characters before the start position will be removed. |
count | Optional. Specifies the number of substitutions to
perform. Default value is -1, which means make all possible substitutions |
compare | Optional. Specifies the string comparison to use.
Default is 0 Can have one of the following values:
|
ตัวอย่าง
ตัวอย่าง 1
แทนที่คำว่า "สวย" ด้วย "มหัศจรรย์":
<%
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
แทนที่ 2 ตัวแรกของตัวอักษร "i" ด้วย "##" โดยเริ่มจากตำแหน่งที่ 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 ที่สมบูรณ์