ฟังก์ชัน InStr ของ VBScript

คำหมายและวิธีใช้

ฟังก์ชัน InStr สามารถกลับค่าตำแหน่งการปรากฏของตัวอักษรในตัวอักษรอื่น

ฟังก์ชัน InStr สามารถกลับค่าดังนี้

  • ถ้า string1 คือ "" (ขนาด 0) - 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])
Parameters Description
start Optional. Specifies the starting position of each search. The default is the first character. If the compare parameter is specified, this parameter must also be specified.
string1 Required. The string to be searched for.
string2 Required. The string to search for.
compare

Required. Specifies the type of string comparison to use. The default is 0. The following values can be used:

  • 0 = vbBinaryCompare - Execute binary comparison.
  • 1 = vbTextCompare - Execute text comparison.

ตัวอย่าง

ตัวอย่าง 1

dim txt,pos
txt="This is a beautiful day!"
pos=InStr(txt,"his")
document.write(pos)

ออกทาง:

2

ตัวอย่าง 2

dim txt,pos
txt="This is a beautiful day!"
A textual comparison starting at position 4
pos=InStr(4,txt,"is",1)
document.write(pos)

ออกทาง:

6

ตัวอย่าง 3

dim txt,pos
txt="This is a beautiful day!"
A binary comparison starting at position 1
pos=InStr(1,txt,"B",0)
document.write(pos)

ออกทาง:

0