Instr Functions
Returns the position where a string appears for the first time in another string.
Instr ([Start,] string1, string2 [, compare])
Parameters
Start
Optional. Value expression, used to set the start position of each search. If omitted, the search starts from the first character. IfStartIf it contains null, an error occurs. IfCompare, You must haveStartParameters.
String1
Required. String expression to be searched.
String2
Required. The string expression to be searched.
Compare
Optional. Indicates the value of the comparison type used to calculate the substring. For values, see the "Settings" section. If omitted, binary comparison is executed.
Set
CompareParameters can have the following values:
Constant |
Value |
Description |
Vbbinarycompare |
0 |
Perform binary comparison. |
Vbtextcompare |
1 |
Execute text comparison. |
Return Value
InstrThe function returns the following values:
If |
Instr return |
String1 0 Length |
0 |
String1 is null |
Null |
String2 0 Length |
Start |
String2 is null |
Null |
String2 not found |
0 |
Find string2 in string1 |
Locate the position that matches the string |
Start> Len (string2) |
0 |
Description
The following example usesInstrSearch string:
Dim searchstring, searchchar, mypossearchstring = "xxpxxpxxpxxp" 'string to search in. searchchar = "p" 'search for "p". Mypos =Instr (4,Searchstring,Searchchar,1)
'A textual comparison starting at position 4. Returns 6.
Mypos =Instr (1,Searchstring,Searchchar,0)
'A binary comparison starting at position 1. Returns 9.
Mypos =Instr (Searchstring,Searchchar)
'Comparison is binary by default (last argument is omitted). Returns 9.
Mypos =Instr (1,Searchstring,"W")
'A binary comparison starting at position 1. returns 0 ("W" is not found ).
Note: Limit BThe function uses the byte data contained in the string, soLimit BThe returned result is not the first occurrence of a character string in another string, but the byte position.