index() Function

The index() function searches through a given str.exp for the occurrence of the character or substring specified by substr.exp and if found, returns the numeric position where the nth occurrence of the substring begins. Null strings are skipped.

Syntax

index(str.exp, substr.exp, num.exp)

Parameter(s)

str.exp

String to search.

substr.exp  

Character or substring to search for in str.exp.

num.exp

Specifies the particular occurrence of the character or substring to find (nth occurrence). If not found, 0 is returned.

Description

NOTE

Searching for a null in any string returns a result of 1.

Example(s)

This returns the value 4 to the variable test, because the first occurrence of d is found in the fourth position of the string.

d.position = index("abcdefg","d",1)

This example exploits the fact that index returns a number indicating the position of the search character. For instance, if response is the letter d, then it goes to statement label 40.

on index("abcdefg",response,1) goto 10,20,30,40,50,60,70

If ans is not null and is either an a, b, c, ab, or bc, the loop is terminated. Notice that the null condition must be tested separately because a null value for ans results in a 1 from the index() function.

loop

input ans,1

until ans#’’ and index(’abc’,ans,1) do repeat

See Also

BASIC Functions, Numeric Expressions, Statements and Functions