ABAP Search string: FIND Statement

  Define Data

OverView


The FIND Statement searches for whether a specific character is included, and if so, returns the position of that character.

Sample Code:Only Search

DATA: V_TEXT TYPE String.

V_TEXT = 'AAAAFINDBBBBB'.

FIND 'FIND' IN V_TEXT.
IF SY-SUBRC = 0.
  WRITE 'OK'.
ENIDF.

Explanation

In this example, the FIND instruction is used to check if there is a character ‘FIND ‘from the variable: V_TEXT. System variable: SY-SUBRC returns ‘0’ if it can be found, and ‘4’ if it cannot be found. Describe each process after branching with IF statement etc. I can do things.

Sample Code:Return search position

DATA: V_TEXT TYPE String,
      V_OFF TYPE I,
      V_CNT TYPE I.

V_TEXT = 'AAAAFINDBBBBB'.

FIND 'FIND' IN V_TEXT MATCH OFFSET V_OFF MATCH LENGTH V_LEN.
IF SY-SUBRC = 0.
  WRITE V_TEXT+V_OFF(V_LEN).
ENIDF.

Explanation

In this example, the FIND command is used, and if the character “FIND” is found in the variable: V_TEXT, the position and length found will be returned. As in the previous case, the system variable: SY-SUBRC is checked, and if found, only the found character is cut out and output to the screen.

Sample Code:Multiple search

DATA: V_TEXT TYPE String,
      V_LEN TYPE I.

V_TEXT = 'AAAAFINDBBBBBFINDCCC'.

FIND ALL OCCURRENCES OF 'FIND' IN V_TEXT MATCH COUNT V_CNT.
IF SY-SUBRC = 0.
  WRITE V_CNT.
ENIDF.

Explanation

In this example, the FIND instruction is used to retrieve all the characters’FIND ‘from the variable: V_TEXT. In the example, the character FIND can be found twice, so the variable V_CNT is set to 2.

FIRST OCCURRENCES will be one search, ALL OCCURRENCES will be all searches. Although omitted in the above two examples, FIRST OCCURRENCES is used if the description is omitted.

Sample Code:Store result into internal table

DATA: V_TEXT TYPE String,
      result_tab TYPE match_result_tab.

V_TEXT = 'AAAAFINDBBBBBFINDCCC'.

FIND ALL OCCURRENCES OF 'FIND' 
IN V_TEXT 
RESULTS result_tab.

Explanation

Honestly, I can’t think of a use, but it is possible to store the result in an internal table by specifying an internal table after RESULTS. The first line stores the position of the first character found, the number of digits, etc., and the second line stores the position of the next character found, the number of digits, etc. You can loop through the internal table and process each character.

Sample Code:Search string from internal table

DATA: itab TYPE TABLE OF string.

FIND ALL OCCURRENCES OF 'FIND'
IN TABLE itab
RESPECTING CASE
RESULTS result_tab.

Explanation

An internal table can also be specified as a search target as in the above example. It can be used as an internal table after reading the file. However, it is also possible to search for each line by looping, so it may be a syntax to use in consideration of performance and simplification of logic.

Summary

This time I introduced the FIND command, but there should be many requirements such as “I want to do this” only when there is a specific description from the characters. When dealing with XML etc., it is often used because it is often judged by the name of the tag. Also, each text (set on orders or purchase slips) can be used as a syntax for extracting a text input by the user and searching for a specific character when processing the text.