ABAP - Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Statements for Character String and Byte String Processing → 

FIND

Quick Reference

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

Example

The simplest form of the statement FIND.

FIND 'bcd' in 'abcde'.
ASSERT sy-subrc = 0.

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 searching for all occurrences, in this case the exception CX_SY_FIND_INFINITE_LOOP is triggered.

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 space before the first character, all intermediate spaces that are not within a match, and the space 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 carried out. 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_codepage=>convert_to( `a b c` ).
DATA(xspc) = cl_abap_codepage=>convert_to( ` ` ).
FIND xspc IN xstr IN BYTE MODE MATCH OFFSET DATA(moff).
ASSERT moff = 1.

Exceptions

Handleable Exceptions

CX_SY_FIND_INFINITE_LOOP

CX_SY_RANGE_OUT_OF_BOUNDS

CX_SY_INVALID_REGEX

CX_SY_REGEX_TOO_COMPLEX



Continue
FIND - pattern
FIND - section_of
FIND - options