Method
InStr
Warning
This item was deprecated in version 2019r2. Please use String.IndexOf as a replacement.
Description
Returns the position of the first occurrence of a string inside another string. The first character is numbered 1.
Usage
result = InStr([start,] source, find)
OR
result = stringVariable.InStr([start,] find)
Part |
Type |
Description |
---|---|---|
result |
The position of the first occurrence of find in source. If the search string cannot be located in source, InStr returns 0. |
|
start |
Optional one-based position from which to begin searching the source string. The default is 0. |
|
source |
Required. String expression being searched. |
|
find |
Required. String expression being sought. |
|
stringVariable |
Any variable of type String. |
Notes
If the find string is not found within the source string, 0 (zero) is returned. If the find string is an empty string, then start is returned. That is, `InStr`("This", "") returns 0 and `InStr`(3, "This","") returns 3.
InStr is case-insensitive, even with accented Roman characters and non-Roman alphabets.
If you need to find the byte position of the find string within the source string or need a case-sensitive function, use the String.IndexOfBytes function.
Sample code
This example uses the InStr function to locate a string within another string.
Dim first As Integer
first = InStr("This is a test", "t") ' returns 1
first = InStr("This is a test", "is") ' returns 3
first = InStr(4, "This is a test", "is") ' returns 6
first = InStr("This is a test", "tester") ' returns 0
Dim s As String
s = "This is a test"
first = s.InStr("test") ' returns 11
Compatibility
All project types on all supported operating systems.
See also
Asc, Chr, String.IndexOfBytes, String.Left, String.Length, String.Middle, String.NthField, String.Right, String.Split, String.Compare functions.