In SAP ABAP, you can reverse a string using different techniques, as ABAP does not provide a built-in REVERSE function. Here are some methods to achieve string reversal:


1. Using String Processing (SHIFT, CONCATENATE)

This method loops through the string from the end to the beginning and constructs the reversed string.

Example

DATA: lv_text TYPE string VALUE 'ABAP',
lv_result TYPE string,
lv_char TYPE char1,
lv_len TYPE i.
lv_len = strlen( lv_text ). " Get string length
DO lv_len TIMES.
lv_char = lv_text+lv_len-1(1). " Extract character from the end
CONCATENATE lv_result lv_char INTO lv_result. " Append to result
lv_len = lv_len - 1.
ENDDO.
WRITE: lv_result. " Output: PABA

2. Using CL_ABAP_STRING_UTILITIES=>REVERSE (Newer ABAP Versions)

If you’re using ABAP 7.40+, SAP provides a built-in method for reversing strings.

Example

DATA(lv_text) = 'ABAP'.
DATA(lv_reversed) = CL_ABAP_STRING_UTILITIES=>REVERSE( lv_text ).
WRITE: lv_reversed. " Output: PABA

3. Using an Internal Table (Character-by-Character)

This method converts the string into a table of characters, then reads it in reverse order.

Example

DATA: lv_text TYPE string VALUE 'ABAP',
lt_chars TYPE TABLE OF char1,
lv_result TYPE string.
DATA(lv_len) = strlen( lv_text ).
" Split string into characters
DO lv_len TIMES.
APPEND lv_text+sy-index-1(1) TO lt_chars.
ENDDO.
" Convert internal table back to string
LOOP AT lt_chars INTO DATA(lv_char).
CONCATENATE lv_result lv_char INTO lv_result.
ENDLOOP.
WRITE: lv_result. " Output: PABA

Best Method?