Finding Character Strings in Internal Tables 

To find a string in a line of an index table, use the following statement:

SEARCH <itab> FOR <str> <options>.

The statement searches the internal table <itab> for the character string <str>. If the search is successful, SY-SUBRC is set to 0, and SY-TABIX is set to the index of the table line in which the string was found. SY-FDPOS contains the offset position of the string in the table line. Otherwise, SY-SUBRC is set to 4.

The statement treats all table lines as type C fields, regardless of their actual line type. There is no conversion. The search string <str> can have the same form as for a normal string search in a field.

The different options (<options>) for the search in an internal table <itab> are:

Field <c> is searched for a word containing the string in <str>. The characters can be separated by other characters. The first letter of the word and the string <str> must be the same.

Searches table <itab> for <str>, starting at line <lin 1 >.<lin 1 > can be a variable.

Searches table <itab> for <str> up to line <lin 2 >.<lin 2 > can be a variable.

If the search string is found, all the characters in the search string (and all the characters in between when using ABBREVIATED) are converted to upper case.

This statement only works with index tables. There is no corresponding statement for hashed tables.

Example

DATA: BEGIN OF LINE,
        INDEX   TYPE I,
        TEXT(8) TYPE C,
      END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY INDEX.

DATA NUM(2) TYPE N.

DO 10 TIMES.
  LINE-INDEX = SY-INDEX.
  NUM = SY-INDEX.
  CONCATENATE 'string' NUM INTO LINE-TEXT.
  APPEND LINE TO ITAB.
ENDDO.

SEARCH ITAB FOR 'string05' AND MARK.

WRITE: / '''string05'' found at line', (1) SY-TABIX,
         'with offset', (1) SY-FDPOS.

SKIP.

READ TABLE ITAB INTO LINE INDEX SY-TABIX.
WRITE: / LINE-INDEX, LINE-TEXT.

The output is:

'string05' found at line 5 with offset 4

         5 STRING05

The offset of the string found in the table is determined by the width of the first table column, which has type I and length 4. The option AND MARK changes the table contents in the corresponding line.