InStr Function

Purpose

Searches a string expression for occurrence of a substring

Syntax

i = InStr(a, b [,m%] [, compmode])

i = InStr([m%], a, b [, compmode])

a, b: string expression or Variant expression
m%, compmode: iexp

Description

InStr() searches through the string or string in Variant expression a starting from position m% for the substring b. If m% is not given, the search starts from the first character in string a. The compare compmode indicates how the search for b inside a is to be performed. The compmode can take the same values as the Mode Compare statement.

compmode meaning
0 binary compare (default)
1 case insensitive
-2 converts both strings to uppercase before searching
-3 converts both strings to lowercase before searching

If b isn't found or if b="" the command returns 0.

Example

Debug.Show

Trace InStr("Hello GFA", "ll", 2)          // Prints 3

Trace InStr("Hello GFA", "ll")             // Prints 3

Trace InStr("Hello GFA", "ll", 4)          // Prints 0

Trace InStr(0, "Hello GFABASIC", "LL", 1// 3

Trace InStr(0, "Hello GFABASIC", "LL", 0// 0

Trace InStr("Hello GFABASIC", "LL", 0, -2) // 3

Trace InStr("Hello GFABASIC", "LL", 0, -3) // 3

See Also

EndsWith, FindFirstOf, FindLastOf, Left$, Mid$, Mode, Right$, RInStr(), StartsWith

{Created by Sjouke Hamstra; Last updated: 18/12/2022 by James Gaite}