INDEX()

INDEX(), INDEXS()

Top  Previous  Next

 

The INDEX() function returns the position of a specified occurrence of a substring within a string. The INDEXS() function is similar to INDEX() but operates on each element of a dynamic array element separately, locating the required occurrence of substring and returning a similarly structured dynamic array of results.

 

 

Format

 

INDEX(string, substring {, occurrence})

 

where

 

stringis the string in which the search is to occur.

 

substringevaluates to the substring to be located.

 

occurrenceevaluates to the position of the occurrence of the substring to be located. If omitted, this argument defaults to 1.

 

 

The INDEX() function locates the specified occurrence of substring within string and returns its character position.

 

If occurrence is less than one or the desired occurrence of substring is not found, the INDEX() function returns zero.

 

If substring is null, the value of occurrence is returned.

 

Use of the $NOCASE.STRINGS compiler directive makes the comparison case insensitive.

 

The default behaviour of the INDEX() function is that overlapping substrings are not allowed. Thus

X = INDEX("aaaaaaaaa", "aa", 3)

yields a result of 5.

 

The INDEX.OVERLAP setting of the $MODE compiler directive can be used to select an alternative behaviour in which substrings may overlap. The same example would then yield a result of 3.

 

 

Examples

 

N = INDEX(S, "*", 3)

 

This statement assigns N with the character position of the third asterisk in variable S.

 

 

S = "ABABABABABAB"

N = INDEX(S, "ABA", 2)

 

Because substrings are defined not to overlap, this sets N to 5.