INSTR function

Purpose

Search a string for the first occurrence of a specified character or string.

Syntax

y& = INSTR([n&,] MainString, [ANY] MatchString)

Remarks

INSTR returns the position of MatchString within MainString.

n& is an optional Long-integer expression, and MainString and MatchString are string variables, expressions, or string equates/constants.

If n& is greater than zero (positive), MainString is searched from left to right, starting at character position n&, for the first occurrence of MatchString.

If n& is less than zero (negative), MainString is searched from right to left for the first occurrence of MatchString.  If -1, the search begins at the last position.  If -2, the search begins at the second to last position, and so forth.

x& = INSTR(-2, "efcdef", ANY "ef")

This returns 5.

If MatchString is not found in the specified portion of MainString, or n& evaluates outside of MatchString, INSTR returns 0.  If MatchString is null (an empty string), INSTR returns 1 (if n& not specified) or n& (if n& is specified).

ANY

If the ANY keyword is included, MatchString specifies a list of single characters to be searched for individually, a match on any one of which will conclude the search operation, and return the position of the match.

INSTR is case-sensitive, meaning that upper-case and lower-case letters must match exactly in MatchString and MainString.

Restrictions

If n& is zero, INSTR will return zero.

See also

EXTRACT$, LCASE$, LEFT$, LTRIM$, MID$, RIGHT$, RTRIM$, TALLY, TRIM$, UCASE$, VERIFY

Example

' x$ = first command-line argument, assuming

' spaces, commas, periods, and tabs are valid

' delimiters

IF INSTR(COMMAND$, ANY " ,." + CHR$(9)) > 0 THEN

 x$ = "There is more than one command-line argument"

ELSE

 x$ = "There is at most one command-line argument"

END IF