The
line type used in the internal table declaration must contain at least three components any text type.
The routine should replace all occurrences of "space" by a "_" (underscore) using field symbols
(and not work areas). Print the table contents before and after calling the routine. The internal table
must be populated with at least 10 records and contemplating some fields that have "space" in all
field values, other records containing spaces in just a few fields and other records without spaces at
all.
Solution:
REPORT z_abap101_064.
TYPES: BEGIN OF ty_line,
comp1 TYPE c LENGTH 10,
comp2 TYPE string,
comp3 TYPE c LENGTH 5,
END OF ty_line.
TYPES: ty_tt_line TYPE TABLE OF ty_line.
*&---------------------------------------------------------------------*
*& Form replace_spaces
*&---------------------------------------------------------------------*
* Replaces all occurrences of "space" by a "_" (underscore)
* using field symbols (not work areas).
*----------------------------------------------------------------------*
* -->CH_ITAB text
*----------------------------------------------------------------------*
FORM replace_spaces CHANGING ch_itab TYPE ty_tt_line.
FIELD-SYMBOLS <line> TYPE ty_line.
LOOP AT ch_itab ASSIGNING <line>.
REPLACE ALL OCCURRENCES OF REGEX '\s' IN <line>-comp1 WITH '_' IN CHARACTER
MODE.
REPLACE ALL OCCURRENCES OF REGEX '[[:space:]]' IN <line>-comp2 WITH '_' IN
CHARACTER MODE.
REPLACE ALL OCCURRENCES OF REGEX '\s' IN <line>-comp3 WITH '_' IN CHARACTER
MODE.
ENDLOOP.
ENDFORM. "replace_spaces
*&---------------------------------------------------------------------*
*& Form print_itab
*&---------------------------------------------------------------------*
* Prints internal table contents
*----------------------------------------------------------------------*
* -->US_ITAB text
*----------------------------------------------------------------------*
FORM print_itab USING us_itab TYPE ty_tt_line.
FIELD-SYMBOLS <line> TYPE ty_line.
LOOP AT us_itab ASSIGNING <line>.
WRITE: <line>-comp1 COLOR 1. NEW-LINE.
WRITE: <line>-comp2 COLOR 2. NEW-LINE.
WRITE: <line>-comp3 COLOR 3. NEW-LINE.
WRITE /.
ENDLOOP.
ENDFORM. "print_itab
START-OF-SELECTION.
DATA itab TYPE ty_tt_line.
DATA wa TYPE ty_line.
wa-comp1 = 'ABAP 101'.
wa-comp2 = 'One Two Three Four Five Six Seven Eight Nine'.
wa-comp3 = '12345'.
APPEND wa TO itab.
CLEAR wa.
wa-comp1 = 'ABAP101'.
wa-comp2 = 'One/Two/Three/Four Five/Six/Seven/Eight/Nine'.
wa-comp3 = '12 45'.
APPEND wa TO itab.
CLEAR wa.
wa-comp1 = ' '.
wa-comp2 = 'One/Two/Three/Four+=_-Five/Six/Seven/Eight/Nine'.
wa-comp3 = ''.
APPEND wa TO itab.
CLEAR wa.
WRITE: 'Before replace'. NEW-LINE.
PERFORM print_itab
USING
itab.
PERFORM replace_spaces
CHANGING
itab.
WRITE: /, 'After replace'. NEW-LINE.
PERFORM print_itab
USING
itab.



Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Consultant
Contact : serkurumsal@yandex.com