Returns the position of the first occurrence of one string within another.
InStr([start,]string1,string2[,compare])
The
InStr function searches a string from left-to-right, and returns the location of the first occurrence of
string2 encountered. The location returned is a positive integer counting from left-to-right, with the first (leftmost) character of the string being location 1.
If the
start value is equal to or less than the location of the first (leftmost) character of
string2,
InStr finds the first instance of
string2, searching forewards from that point, and returns the location of the first (leftmost) character of
string2. If the
start value is greater than the location of the first (leftmost) character of
string2,
InStr returns 0. If the
start value is omitted,
InStr searches the entire string and returns the first
string2 location found. If the
start value is greater than the
string1 length,
InStr returns 0. If the
start value is a negative number,
InStr returns 0.
Dim SearchString, SearchChar
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
' A textual comparison starting at position 4. Returns 6.
PRintln Instr(4, SearchString, SearchChar, 1)
' A binary comparison starting at position 1. Returns 9.
Println Instr(1, SearchString, SearchChar, 0)
' Comparison is binary by default (last argument is omitted).
Println Instr(SearchString, SearchChar) ' Returns 9.
' A binary comparison starting at position 1. Returns 0 ("W" is not found).
Println Instr(1, SearchString, "W")
Println "-1: ",InStr(-1,"abcdefg","bc") ' Returns 2
Println "0: ",InStr(0,"abcdefg","bc") ' Returns 2
Println "1: ",InStr(1,"abcdefg","bc") ' Returns 2
Println "2: ",InStr(2,"abcdefg","bc") ' Returns 2
Println "3: ",InStr(3,"abcdefg","bc") ' Returns 0
Println "6: ",InStr(6,"abcdefg","bc") ' Returns 0
Println "7: ",InStr(7,"abcdefg","bc") ' Returns 0
Println "8: ",InStr(8,"abcdefg","bc") ' Returns 0