ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Character String and Byte String Processing → Statements for Character String and Byte String Processing
FIND
Other versions: 7.31 | 7.40 | 7.54
Syntax
FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]
pattern
IN [section_of] dobj
[IN {CHARACTER|BYTE} MODE]
[find_options].
Extras
1. ... {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF
2. ... [IN {CHARACTER|BYTE} MODE]
Effect
The operand dobj
is searched for the character or byte sequence specified by the search string pattern
.
The additions FIRST OCCURRENCE
and ALL OCCURRENCES
determine whether all occurrences or only the first one is searched. The addition
section_of
can be used to restrict the search range. The search is terminated
if the search pattern was found for the first time, or if all search patterns were found in the entire
search area, or if the end of the search area was reached. The search result is communicated by setting
sy-subrc
. The addition MODE
determines a character
or byte string is processed, and the addition find_options
provides additional options for controlling and analyzing the statement.
When a character string is processed, dobj
is a
character-like expression
position and the blanks are respected for dobj
operands of a fixed length.
System Fields
sy-subrc | Meaning |
---|---|
0 | The search pattern was found at least once in the search range. |
4 | The search pattern was not found in the search range. |
Notes
-
The statement
FIND IN TABLE
is available for searching in internal tables. -
Search functions can be used to search in a string in an operand position. They mask some of the functions of the statement
FIND
. -
The statement
FIND
and the search functions can be quicker than the relational operatorCS
by some magnitude.
Addition 1
... {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF
Effect
The optional addition {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF
defines whether all or only the first occurrence
of the search pattern is searched. If the addition FIRST OCCURRENCE
or none of the additions is specified, only the first occurrence is found. Otherwise, all occurrences are found.
If substring
is an empty string in the
pattern or is of type c
, n
, d
,
or t
and only contains blanks, the place in front of the first character
or byte of the search range is found when searching for the first occurrence. If searches for all occurrences the exception CX_SY_FIND_INFINITE_LOOP is raised in this case.
If regex
contains a regular expression
in the
pattern that matches the empty character string, the
search for the first occurrence also finds the place before the first character. When searching for
all occurrences in this case, the search finds the place before the first character, all intermediate places that are not within a match, and the place after the last character.
Example
All three occurrences of the letter "a" are searched for and found.
FIND ALL OCCURRENCES OF 'a' in 'ababa' MATCH COUNT DATA(mcnt).
ASSERT mcnt = 3.
Addition 2
... IN {CHARACTER|BYTE} MODE
Effect
The optional addition IN {CHARACTER|BYTE} MODE
determines whether
character string or byte string processing
is performed. If the addition is not specified, character string processing is performed. Depending
on the processing type, dobj
and substring
in pattern
must be character-like or byte-like. If
regular expressions are used in pattern
, only character string processing is permitted.
Example
Finds the first byte that represents a blank space in the code page UTF-8.
DATA(xstr) =
cl_abap_conv_codepage=>create_out( )->convert( `a b c` ).
DATA(xspc) =
cl_abap_conv_codepage=>create_out( )->convert( ` ` ).
FIND xspc IN xstr IN BYTE MODE MATCH OFFSET DATA(moff).
ASSERT moff = 1.
Exceptions
Handleable Exceptions
CX_SY_FIND_INFINITE_LOOP
-
Cause: Substring of length 0 creates an endless loop when searching for all occurrences.
Runtime error:FIND_INFINITE_LOOP
CX_SY_RANGE_OUT_OF_BOUNDS
-
Cause: Illegal offset or length specified in the addition
SECTION OF
.
Runtime error:REFI_WRONG_SECTION
CX_SY_INVALID_REGEX
-
Cause: Invalid expression after the addition
REGEX
.
Runtime error:INVALID_REGEX
CX_SY_REGEX_TOO_COMPLEX
-
Cause: More information: Exceptions in Regular Expressions.
Runtime error:REGEX_TOO_COMPLEX