1. SEARCH
f FOR g.
2. SEARCH itab FOR g.
Depending on whether byte or character string processing is carried out, the operands can only be byte-type
or character-type (see Overview).
See Only character fields allowed in string processing.
In some cases, the syntax rules that apply to Unicode programs are different
than those for non-Unicode programs. For more information, see Character String Processing.
SEARCH f FOR g.
1. ... ABBREVIATED
2. ...
STARTING AT n1
3. ...
ENDING AT n2
4.
... AND MARK
5. ...
IN BYTE MODE
6. ... IN CHARACTER MODE
Searches the contents of f for the string in the field g. This string can have any of the following formats:
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.
... ABBREVIATED
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.
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 in 'California',
but 'Clfrn' and 'California' begin with the same letter.
... STARTING AT n1
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.
... ENDING AT n2
Searches the field f up to the position n2.
... AND MARK
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.
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'
... IN BYTE MODE
This addition can be used in combination with the additions STARTING
AT and ENDING AT. All fields must be of the
type X or XSTRING. Only 'str' is allowed as a search pattern.
... IN CHARACTER MODE
The addition is optional and corresponds to the default setting (see above).
SEARCH itab FOR g.
1. ... ABBREVIATED
2. ...
STARTING AT line1
3. ...
ENDING AT line2
4.
... AND MARK
5. ...
IN BYTE MODE
6. ... IN CHARACTER MODE
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.
The statement does not search the header line of an internal table itab.
... ABBREVIATED
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.
... STARTING AT line1
Searches the internal table
itab starting from line line1
to the end. line1 can be a field that contains the corresponding values.
... ENDING AT line2
Searches the internal table itab up to the line line2.
... AND MARK
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.
Let T be an internal table which is empty:
TYPES: BEGIN OF T_TYPE,
LINE(80),
END
OF T_TYPE.
DATA: T TYPE STANDARD TABLE OF T_TYPE WITH
NON-UNIQUE DEFAULT KEY,
WA_T TYPE T_TYPE.
MOVE 'Alaska
Texas ' TO WA_T.
APPEND WA_T TO T.
MOVE 'California Arizona ' TO WA_T.
APPEND WA_T TO 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'
... IN BYTE MODE
This addition can be used in combination with the additions STARTING
AT and ENDING AT. All fields must be of the
type X or XSTRING. Only 'str' is allowed as a search pattern.
... IN CHARACTER MODE
The addition is optional and corresponds to the default setting (see above).
Performance:
Searching generically for a string in an internal table is much more runtime-intensive (
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.
REPLACE, OVERLAY,
SHIFT, SPLIT,
TRANSLATE, FIND
Searching in Strings
Searching Table Lines for Strings