In SAP ABAP, you can check if a string contains a specific substring using various methods:
1. Using FIND
statement
DATA: lv_string TYPE string VALUE 'Hello ABAP World', lv_substring TYPE string VALUE 'ABAP', lv_result TYPE i.
FIND lv_substring IN lv_string.IF sy-subrc = 0. WRITE: 'Substring found'.ELSE. WRITE: 'Substring not found'.ENDIF.
- If
sy-subrc = 0
, it means the substring was found. - If
sy-subrc ≠ 0
, the substring was not found.
2. Using CS
(Contains String) Operator (from ABAP 7.40)
DATA: lv_string TYPE string VALUE 'Hello ABAP World'.
IF lv_string CS 'ABAP'. WRITE: 'Substring found'.ELSE. WRITE: 'Substring not found'.ENDIF.
- The
CS
operator checks if the string contains the given substring.
3. Using MATCHES
with Regular Expressions (from ABAP 7.40)
DATA: lv_string TYPE string VALUE 'Hello ABAP World'.
IF lv_string MATCHES '.*ABAP.*'. WRITE: 'Substring found'.ELSE. WRITE: 'Substring not found'.ENDIF.
- This method is useful if you need pattern matching.
Which method you choose depends on your ABAP version and requirements! 🚀