InStr, InStrB (functions)

Syntax

InStr([start,] search, find [,compare])

InStrB([start,] search, find [,compare])

Description

Returns the first character position of string find within string search.

Comments

The InStr function takes the following parameters:

 

Parameter

Description

 

start

Integer specifying the character position where searching begins. The start parameter must be between 1 and 32767.

If this parameter is omitted, then the search starts at the beginning (start = 1).

 

search

Text to search. This can be any expression convertible to a String.

 

find

Text for which to search. This can be any expression convertible to a String.

 

compare

Integer controlling how string comparisons are performed:

 

 

0

String comparisons are case-sensitive.

 

 

1

String comparisons are case-insensitive.

 

 

Any other value

A runtime error is produced.

 

 

If this parameter is omitted, then string comparisons use the current Option Compare setting. If no Option Compare statement has been encountered, then Binary is used (i.e., string comparisons are case-sensitive).

 

If the string is found, then its character position within search is returned, with 1 being the character position of the first character.

 

The InStr and InStrB functions observe the following additional rules:

If either search or find is NULL, then NULL is returned.

If the compare parameter is specified, then start must also be specified. In other words, if there are three parameters, then it is assumed that these parameters correspond to start, search, and find.

A runtime error is generated if start is NULL.

A runtime error is generated if compare is not 0 or 1.

If search is Empty, then 0 is returned.

If find is Empty, then start is returned. If start is greater than the length of search, then 0 is returned.

A runtime error is generated if start is less than or equal to 0.

 

The InStr and InStrB functions operate on character and byte data respectively. The Instr function interprets the start parameter as a character, performs a textual comparisons, and returns a character position. The InStrB function, on the other hand, interprets the start parameter as a byte position, performs binary comparisons, and returns a byte position.

On SBCS platforms, the InStr and InStrB functions are identical.

Example

This example checks to see whether one string is in another and, if it is, then it copies the string to a variable and displays the result.

 

Sub Main()
  a$ = "This string contains the name Stuart and other characters."
  x% = InStr(a$,"Stuart",1)
  If x% <> 0 Then
    b$ = Mid(a$,x%,6)
    MsgBox b$ & " was found."
    Exit Sub
  Else
    MsgBox "Stuart not found."
  End If
End Sub

See Also

Mid, Mid$ (functions); Option Compare (statement); Item$ (function); Word$ (function); Line$ (function).

 

 

 

 

More information

I