Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
manijangiti
Discoverer
0 Kudos
321


This blog provides a smart way to clean special characters like @, #, !, and , from internal table data.
It helps when exporting data to CSV files, especially for AL11 directory usage.
Instead of writing code for each field, we clean all fields in one loop using field symbols.
RTTI (Runtime Type Info) is used to make it dynamic for any table.
This makes your exported data neat, clean, and ready for use.

Before Replacing
2a8f888a-14ee-47a0-8ca4-938a766ae396.jpg

Code :

REPORT ZREPLACE_INTERNAL_TABLE.

"Define structure TY_DATA for sample person data
TYPES: BEGIN OF TY_DATA,
         NAME     TYPE STRING,
         CITY     TYPE STRING,
         EMAIL    TYPE STRING,
         ADDRESS  TYPE STRING,
         COMMENTS TYPE STRING,
       END OF TY_DATA.

"Internal table and work area to hold the data
DATA: LT_DATA TYPE TABLE OF TY_DATA,
      LS_DATA TYPE TY_DATA.

"Reference for SALV display
DATA: LT_FACTORTY  TYPE REF TO CL_SALV_TABLE.

"Sample data containing unwanted characters: @, #, !, ,
LT_DATA = VALUE #(
  ( name = 'Mani@ Yadav!' city = 'Hydera#bad' email = 'mani@yadav@example.com' address = 'Road No, 1, Banjara Hills' comments = 'Good@ Person!' )
  ( name = 'Anil Kumar' city = 'Mum#bai' email = 'anil.kumar@exa!mple.com' address = 'Apt@ 123, Green Society' comments = 'Very# Good, Employee' )
  ( name = 'Ravi Teja!' city = 'Hyder@abad' email = 'ravi.teja#@example.com' address = 'Street 45, Jubilee@ Hills' comments = 'Nice, and Reliable!' )
  ( name = 'Sushil Kumar' city = 'Chen@nai' email = 'sushil@kumar@example.com' address = 'Block 7, Ap@artment 3' comments = 'Great, Wo!rker' )
  ( name = 'Kavita@ Lal' city = 'Pune, City' email = 'kavita.lal#@example.com' address = 'MG Road@, Sector 2!' comments = 'Smart and@ Intellig!ent' )
  ( name = 'Arjun Mehta' city = 'Kolkata' email = 'arjun!mehta@example.com' address = 'Sector 9@, Block B' comments = 'Honest@ and Helpful!' )
  ( name = 'Deepa Agarwal#' city = 'Jai!pur' email = 'deepa@agarwal@example.com' address = 'Colony 5@, Lane 3' comments = 'Re#liable Friend!' )
  ( name = 'Vikrant Singh' city = 'Goa! Beach@' email = 'vikrant@singh#@example.com' address = 'Villa 22, P@alolem' comments = 'Social@ Worker#' )
  ( name = 'Neha Sharma' city = 'Ind@ore# MP!' email = 'neha@sharma@example.com' address = 'Home@ 98, New Town!' comments = 'Quick@ L!ear#ner' )
  ( name = 'Priya! Verma' city = 'De@lhi' email = 'priya.verma@example.com' address = 'Flat 12B, Lotus En@clave!' comments = 'Team, Play#er!' )
).

"Characters we want to remove from all fields
DATA: LT_UNWANTED TYPE STANDARD TABLE OF STRING WITH EMPTY KEY,
      LV_CHAR     TYPE STRING.

APPEND '@' TO LT_UNWANTED.
APPEND '#' TO LT_UNWANTED.
APPEND '!' TO LT_UNWANTED.
APPEND ',' TO LT_UNWANTED.

FIELD-SYMBOLS: <FS_DATA>  TYPE ANY,
               <FS_FIELD> TYPE ANY.

"Loop through each row in the internal table
LOOP AT LT_DATA ASSIGNING <FS_DATA>.

  "Get runtime structure description using RTTI
  DATA(LO_TABLE_DESCR) = CAST CL_ABAP_TABLEDESCR( CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( LT_DATA ) ).
  DATA(LO_LINE_DESCR)  = CAST CL_ABAP_STRUCTDESCR( LO_TABLE_DESCR->GET_TABLE_LINE_TYPE( ) ).
  DATA(LT_COMPONENTS)  = LO_LINE_DESCR->COMPONENTS.

  "Loop through each field in the structure
  LOOP AT LT_COMPONENTS INTO DATA(LS_COMPONENT).
    ASSIGN COMPONENT LS_COMPONENT-NAME OF STRUCTURE <FS_DATA> TO <FS_FIELD>.

    "Replace all unwanted characters with space
    IF SY-SUBRC = 0 AND <FS_FIELD> IS ASSIGNED.
      LOOP AT LT_UNWANTED INTO LV_CHAR.
        REPLACE ALL OCCURRENCES OF LV_CHAR IN <FS_FIELD> WITH ' '.
      ENDLOOP.
    ENDIF.
  ENDLOOP.
ENDLOOP.

"Display the cleaned internal table using SALV Grid
CALL METHOD CL_SALV_TABLE=>FACTORY
  IMPORTING
    R_SALV_TABLE = LT_FACTORTY
  CHANGING
    T_TABLE      = LT_DATA.

LT_FACTORTY->DISPLAY( ).

"Debugger breakpoint for inspection (optional)
BREAK-POINT.


Final Output after Replacing

ce85d46a-cb8e-4e6e-8755-ad6b2181d0b7.jpg
ABAP Development