ฟังก์ชันVBScript InStr
❮ การอ้างอิง VBScript ที่สมบูรณ์
ฟังก์ชัน InStr จะคืนค่าตำแหน่งของการเกิดขึ้นครั้งแรกของสตริงหนึ่งภายในอีกสตริงหนึ่ง
ฟังก์ชัน InStr สามารถคืนค่าต่อไปนี้:
- หาก string1 เป็น "" - InStr จะคืนค่า 0
- หาก string1 เป็น Null - InStr จะคืนค่า Null
- ถ้า string2 เป็น "" - InStr จะคืนค่า start
- หาก string2 เป็น Null - InStr จะคืนค่า Null
- หากไม่พบ string2 - InStr จะคืนค่า 0
- หากพบ string2 ภายใน string1 - InStr จะส่งคืนตำแหน่งที่พบที่ตรงกัน
- ถ้า start > Len(string1) - InStr คืนค่า 0
เคล็ดลับ:ดูฟังก์ชัน InStrRev ด้วย
ไวยากรณ์
InStr([start,]string1,string2[,compare])
Parameter | Description |
---|---|
start | Optional. Specifies the starting position for each search. The search begins at the first character position (1) by default. This parameter is required if compare is specified |
string1 | Required. The string to be searched |
string2 | Required. The string expression to search for |
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(InStr(txt,"beautiful"))
%>
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
11
ตัวอย่าง 2
ค้นหาตัวอักษร "i" โดยใช้ตำแหน่งเริ่มต้นต่างกัน:
<%
txt="This is a beautiful day!"
response.write(InStr(1,txt,"i") & "<br />")
response.write(InStr(7,txt,"i") & "<br />")
%>
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
3
16
ตัวอย่างที่ 3
การหาตัวอักษร "t" พร้อมการเปรียบเทียบแบบข้อความและเลขฐานสอง:
<%
txt="This is a beautiful day!"
response.write(InStr(1,txt,"t",1) & "<br />")
response.write(InStr(1,txt,"t",0) & "<br />")
%>
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
1
15
❮ การอ้างอิง VBScript ที่สมบูรณ์