InStr
 
Locates the first occurrence of a substring or character within a string

Syntax

Declare Function InStr Overload ( ByRef str As String, [ Any ] ByRef substring As String ) As Integer
Declare Function InStr ( ByRef str As WString, [ Any ] ByRef substring As WString ) As Integer
Declare Function InStr ( ByVal start As Integer, ByRef str As String, [ Any ] ByRef substring As String ) As Integer
Declare Function InStr ( ByVal start As Integer, ByRef str As WString, [ Any ] ByRef substring As WString ) As Integer

Usage

first = InStr( [ start, ] str, [ Any ] substring )

Parameters

str
The string to be searched.
substring
The substring to find.
start
The position in str at which the search will begin.

Return Value

The position of the first occurrence of substring in str.

Description

Locates the position of the first occurrence of a substring or character within a string. In the first form of InStr, the search begins at the first character.

Zero (0) is returned if substring is not found, either str or substring are empty strings, or start < 0.

If the Any keyword is specified, InStr returns the first occurrence of any character in substring.

Example

' It will return 4
Print InStr("abcdefg", "de")

' It will return 0
Print InStr("abcdefg", "h")

Dim test As String
Dim idx As Integer

test = "abababab"
idx = InStr(test, "b")

Do While idx > 0 'if not found loop will be skipped
    Print """b"" at " & idx
    idx = InStr(idx + 1, Test, "b")
Loop

 
Platform Differences

  • DOS does not support the wide-character string versions of InStr.

Differences from QB

  • QB returns start if search is a zero length string.
  • QB does not support Unicode.

See also
'A Unicode example:
dim text as wstring*20
text = "Привет, мир!"
print instr(text,"ет") ' displays 5