INDEX function
Syntax
INDEX (string, substring, occurrence)
Description
Use the INDEX function to return the starting character position for the specified occurrence of substring in string.
string is an expression that evaluates to any valid string. string is examined for the substring expression.
occurrence specifies which occurrence of substring is to be located.
When substring is found and if it meets the occurrence criterion, the starting character position of the substring is returned. If substring is an empty string, 1 is returned. If the specified occurrence of the substring is not found, or if string or substring evaluate to the null value, 0 is returned.
If occurrence evaluates to the null value, the INDEX function fails and the program terminates with a run-time error message.
PICK, IN2, and REALITY Flavors
In PICK, IN2, and REALITY flavor accounts, the search continues with the next character regardless of whether it is part of the matched substring. Use the COUNT.OVLP option of the $OPTIONS statement to get this behavior in IDEAL and INFORMATION flavor accounts.
Example
Q='AAA11122ABB1619MM'
P=INDEX(Q,1,4)
PRINT "P= ",P
*
X='XX'
Y=2
Q='P1234XXOO1299XX00P'
TEST=INDEX(Q,X,Y)
PRINT "TEST= ",TEST
*
Q=INDEX("1234",'A',1)
PRINT "Q= ",Q
* The substring cannot be found.
*
POS=INDEX('222','2',4)
PRINT "POS= ",POS
* The occurrence (4) of the substring does not exist.
This is the program output:
P= 12
TEST= 14
Q= 0
POS= 0