INDEX Function

Purpose

Searches a string for a specified substring and returns a fixed binary integer value indicating its position.

Syntax

INDEX(s,c,l)

Parameters

s and c are both either character or bit strings, and c may be a substring of s. l is a third optional parameter which specifies the location in s from which to start searching.

Description

The INDEX function searches a string, s, for a specified substring, c, and returns an integer value indicating the position of c within s. l specifies the location in the string from which the search begins.

If either s or c is a null string, the result is zero. If the substring c is not contained within s, the result is zero; otherwise, the result is an integer indicating the position within s of the leftmost character or bit (if s and c are bit strings) of the substring c. The result precision is Fixed Binary(15) unless the -bifprec compiler option overrides it.

The third optional argument l must have a computational type and if it is used, the result precision is Fixed Binary(31,0). If l is less than 1 or is greater than length(s), the result is zero.

Examples

Example of INDEX(s,c):

dcl s char (40) ;
    dcl i fixed bin (15);

    s = 'dmf plb cp js acb ltt dsa';
    i = index(s, 'd');
    put skip list (substr(s, i, 3));

will print:

dmf

Example of INDEX(s,c,l):

dcl s char (40) ;
    dcl i fixed bin (15);

    s = 'dmf plb cp js acb ltt dsa';
    i = index(s, 'd', 5);
    put skip list (substr(s, i, 3));

will print:

dsa

Restrictions

None.