Domain: String functions
The INDEX( ) function indicates the position of a search string within the overall string. The overall string can be searched from left to right or from right to left; the result value always relates to the beginning of the overall string.
Format
INDEX( ) |
STRING = string_expression1 ,PATTERN = string_expression2 ,DIRECTION = *FORWARD / *REVERSE ,BEGIN-COLUMN = 1 / arithm_expression ,END-COLUMN = *LAST / arithm_expression |
Result type
INTEGER
Input parameters
STRING = string_expression1
Overall string to be searched.
PATTERN = string_expression2
Search string to be located in the overall string.
DIRECTION =
Search direction
DIRECTION = *FORWARD
The overall string is searched in a forward direction, i.e. from left to right.
DIRECTION = *REVERSE
The overall string is searched in reverse, i.e. from right to left.
BEGIN-COLUMN =
Seen from the start of the overall string, the search operation is restricted to a certain range of columns. The first character in the overall string from which the search for the search string begins is specified.
The string being searched is empty if the overall string contains fewer characters than specified for BEGIN-COLUMN.
BEGIN-COLUMN = 1
The search starts from column 1, i.e. the overall string is searched from the beginning.
BEGIN-COLUMN = arithm_expression
The overall string is searched for the search string from the specified column or from this character
END-COLUMN =
Seen from the end of the overall string, the search operation is restricted to a certain range of columns. The last character in the overall string which is included in the search is specified. All subsequent characters are ignored.
END-COLUMN = *LAST
The overall string is searched to the end.
END-COLUMN = arithm_expression
The overall string is searched for the search string up to the specified column or up to and including this character.
Result
Integer
Initial position of the search string in the overall string.
If there are multiple search strings in the overall string, “integer” indicates the first occurrence of the search string in a search from left to right and the last occurrence in a search from right to left.
0
The search string is longer than the overall string, or the search string is not contained in the overall string.
Error messages
SDP0413 ILLEGAL LENGTH SDP0493 Value of operands BEGIN-INDEX, END-INDEX, BEGIN-COLUMN and END- COLUMN must be greater than zero SDP0498 BEGIN-COLUMN must not be greater than END-COLUMN
Example 1
/A = INDEX(STRING = 'ABCDE', PATTERN = 'C') /SHOW-VARIABLE A A = 3 /B = INDEX(STRING = 'ABCDEABC', PATTERN = 'AB') /SHOW-VARIABLE B B = 1 /C = INDEX(STRING = 'ABCDEABC', PATTERN = 'AB', DIRECTION = *REVERSE) /SHOW-VARIABLE C C = 6
Example 2
/STRING = '1080:0:0:0:8:800:200C:417A' / /WRITE-TEXT '- FROM LEFT TO RIGHT -' /START = 1 /REPEAT / WRITE-TEXT '&(START) => &(SUBSTR( STRING, START ))' / START = INDEX( STRING, ':', *FORWARD, START, *LAST ) + 1 /UNTIL ( START == 1 ) / &* AT THE LAST LOOP ITERATION / &* INDEX DOES NOT FIND THE ':' / &* AND RETURNED 0 / &* 1 IS ADDED FROM THIS RETURNED VALUE. / /WRITE-TEXT '- FROM RIGHT TO LEFT -' /END = LENGTH( STRING ) /REPEAT / WRITE-TEXT '&(END) => &(SUBSTR( STRING, 1, END ))' / END = INDEX( STRING, ':', *REVERSE, 1, END ) - 1 /UNTIL ( END <= 0 ) / &* AT THE LAST LOOP ITERATION / &* INDEX DOES NOT FIND THE ':' / &* AND RETURNED 0. / &* 1 IS SUBTRACTED FROM THIS RETURNED VALUE. / /WRITE-TEXT '- SURROUNDING CUT -' /START = 1 /END = LENGTH( STRING ) /REPEAT / TEXT = '&(START):&(END) => ' // - / SUBSTR( STRING, START, END - START + 1 ) / WRITE-TEXT '&(TEXT)' / START = INDEX( STRING, ':', *FORWARD, START, END ) + 1 / END = INDEX( STRING, ':', *REVERSE, START, END ) - 1 /UNTIL ( START > END )
This example shows how a string can be searched step-by-step for separator characters (here colons) and be reduced by the substring which has already been searched. The search and reduction, which is performed in different directions (left to right, right to left and both ways) supplies the following output:
- FROM LEFT TO RIGHT - 1 => 1080:0:0:8:800:200C:417A 6 => 0:0:0:8:800:200C:417A 8 => 0:0:8:800:200C:417A 10 => 0:8:800:200C:417A 12 => 8:800:200C:417A 14 => 800:200C:417A 18 => 200C:417A 23 => 417A - FROM RIGHT TO LEFT - 26 => 1080:0:0:0:8:800:200C:417A 21 => 1080:0:0:0:8:800:200C 16 => 1080:0:0:0:8:800 12 => 1080:0:0:0:8 10 => 1080:0:0:0 8 => 1080:0:0 6 => 1080:0 4 => 1080 - SURROUNDING CUT - 1:26 => 1080:0:0:0:8:800:200C:417A 6:21 => 0:0:0:8:800:200C 8:16 => 0:0:8:800 10:12 => 0:8