string.IndexOf Method

Searches for an exact match of a substring within a string variable and returns the starting position if found (0-n).

...string.IndexOf( string_s, start )

Prerequisites

None

Parameters

string_s

A required String expression.  The String expression can be a String variable, constant, function or method, or a concatenation of these String elements.  This specifies the substring value that must be found within the string value.

start

An optional numeric expression.  This value specifies the first character position that is tested in the string.  If undefined, match testing begins with the first character in string.  Unlike the Instr function, a 0 specifies the first character position in the string.

Remarks

This method searches the value of the string variable for an exact, case sensitive match to the specified string_s value.  The search begins at the character specified by start and continues with successive characters until either the first match is found or the end of the string is encountered.

Depending upon the outcome of the search, the following values in Table 19-98 are returned by this method.

Table 19-98: String Value, Case Sensitive Match

String Values Returned Value

string_s is found in string

Character position where the match begins.  0 indicates matched started at the first character of string.

string has a zero length

-1

string_s has a zero length

start value

string_s not found in string

-1

Examples

Dim stg_a As String                  ' Create string variable
Dim pos As Integer
stg_a = "aBcDeFgHiJkLmNoPqRsTuVwXyZaBcDeFgHiJk"
pos = stg_a.IndexOf("Fg") ' pos will be set to 5
pos = stg_a.IndexOf("FG") ' pos will be set to -1
pos = stg_a.IndexOf("Fg", 10) ' pos will be set to 31

See Also

Strings | Instr Function