SEARCH

SEARCH Syntax Diagram

Variants:

1. SEARCH f FOR g.
2. SEARCH itab FOR g.

Note

As with any string processing statement, all the operands are processed here as type C fields (regardless of type). No internal conversion is performed.


1. ... ABBREVIATED
2. ... STARTING AT n1
3. ... ENDING AT n2
4. ... AND MARK

Effect

Searches the field f for the string in the field g. This string can have any of the following formats:

'str' a character string (trailing blanks are ignored)

'.str.' any character string between the periods

'*str' a word ending with "str", including "str"

'str*' a word beginning with "str", including "str"

You can use any non-alphanumeric character as a separator, that is, spaces, punctuation marks and non-printable characters.

The system does not distinguish between upper and lower case characters.

SY-SUBRC = 0:

The search string g was found in the field f. SY-FDPOS contains the offset of the found string or the found word within the field.

SY-SUBRC = 4:

The search string g was not found in the field f.

Effect

Searches the field f for a word containing the character string specified in the field. Here, the characters specified in g may be separated by other characters in the word. If the string g occurs in a word, the return code in system field SY-SUBRC is set to 0. The first letter of the search string g and of the word must match.

Example

DATA F(50). 
MOVE 'Alaska Texas California' TO F. 
SEARCH F FOR 'Clfrn' ABBREVIATED. 

Here, SY-SUBRC is set to 0, since not only does the string 'Clfrn' occur (separated by other characters) in 'California', but 'Clfrn' and 'California' begin with the same letter.

Effect

Searches the field f starting from the position n1. Here, n1 can also be a field containing the corresponding value. The first character in the field f is in position 1.
When you use the addition STARTING AT , the position specified for the found pattern in SY-FDPOS does not refer to the start of the field, but to the position n1 .

Effect

Searches the field f up to the position n2.

Effect

If the search string g is found, all the characters of the search string and all the characters occurring in between (in the case of SEARCH ABBREVIATED) are converted to upper case in the field f.

Example

DATA F(20) VALUE 'Peter Paul Mary'. 
SEARCH F FOR '*UL' AND MARK. 

SY-SUBRC is now set to 0, since the search string was found in 'Paul'. SY-FDPOS has the value 6, since the character string found starts at the offset 6. Also, the search string is marked, so that the new contents of f are as follows:

'Peter PAUL Mary'

1. ... ABBREVIATED
2. ... STARTING AT lin1
3. ... ENDING AT lin2
4. ... AND MARK

Effect

Searches the internal table itab for the string in field g. The string can have the same format as in variant 1. The value of SY-SUBRC is set to 0, if the search string is found in the table. The system field SY-TABIX then contains the number of the table line where the string was found. Meanwhile, SY-FDPOS specifies the offset of the found string within the table line.

Note

The statement does not search the header line of an internal table itab.

Effect

As with SEARCH ABBREVIATED, searches the internal table itab for a word that contains the character string specified in the field g. Here, the characters specified in g can be separated by other characters in the word. The return code value of the system field SY-SUBRC is set to 0, if the string g occurs in a word. The first letter of the search string g and of the word must match.

Effect

Searches the internal table itab starting from line lin1 to the end. lin1 can be a field that contains the corresponding values.

Effect

Searches the internal table itab up to the line lin2.

Effect

If the search string g is found, all the characters of that search string and all the characters occurring in between (in the case of SEARCH ABBREVIATED) are converted to upper case in the internal table itab.

Example

Let T be an internal table which is empty:

DATA: BEGIN OF T OCCURS 100, 
		LINE(80), 
	END OF T. 
MOVE 'Alaska Texas	 ' TO T. 
APPEND T. 
MOVE 'California Arizona ' TO T. 
APPEND T. 
SEARCH T FOR '*ONA' AND MARK. 

SY-SUBRC is now set to 0 because the search string was found in 'Arizona'. SY-TABIX contains the value 2 because 'Arizona' appears in the second line of the table T. SY-FDPOS is set to 11 because the found character string begins at the offset 11. Also, the search string was marked in the second line in such a way that the contents of that line now look as follows:

'California ARIZONA'

Related

REPLACE, OVERLAY, SHIFT, SPLIT, TRANSLATE

Searching generically for a string in an internal table is much more runtime-intensive (approx. 5000 msn (standardized microseconds)) than searching for 'str' (approx. 20 msn). This was measured using a table with 200 entries and 10 fields.
If you perform a search in a field which is 50 bytes long for '*str' or 'str*', the runtime consumption is approx. 300 msn, whereas searching for 'str' takes about 15 msn.