Skip to content

CL_ABAP_CHAR_UTILITIES

Static class providing named constants for control characters — newline, tab, carriage return, and more.

Purpose

CL_ABAP_CHAR_UTILITIES is a pure-constant class — no instantiation required. It gives you readable names for the control characters you would otherwise embed as hex literals. Use it directly via the class name: CL_ABAP_CHAR_UTILITIES=>NEWLINE.

Key constants

Constant Value Description
NEWLINE \n (LF, hex 0A) Unix line break — use for internal string separators
CR_LF \r\n (hex 0D0A) Windows line break — use for file downloads to Windows
HORIZONTAL_TAB \t (hex 09) Tab character
VERTICAL_TAB hex 0B Vertical tab (rare)
FORM_FEED hex 0C Form feed / page break
BACKSPACE hex 08 Backspace character

Examples

Build a multiline string and display it

REPORT z_char_util_demo.

START-OF-SELECTION.

  " Build a multiline string using NEWLINE constant
  DATA(lv_body) = |Line 1| &&
                  cl_abap_char_utilities=>newline &&
                  |Line 2| &&
                  cl_abap_char_utilities=>newline &&
                  |Line 3|.

  " Split it back into individual lines to verify
  DATA lt_lines TYPE STANDARD TABLE OF string.
  SPLIT lv_body AT cl_abap_char_utilities=>newline INTO TABLE lt_lines.

  LOOP AT lt_lines INTO DATA(lv_line).
    WRITE: / lv_line.
  ENDLOOP.
  " Output:
  " Line 1
  " Line 2
  " Line 3

Generate a CSV file with Windows line endings

REPORT z_char_util_csv.

START-OF-SELECTION.

  " Read some data from a standard SAP table
  SELECT carrid, connid, price, currency
    FROM sflight
    INTO TABLE @DATA(lt_flights)
    UP TO 5 ROWS.

  " Build CSV content using CR_LF for Windows compatibility
  DATA lv_csv TYPE string.
  lv_csv = |CARRID,CONNID,PRICE,CURRENCY| && cl_abap_char_utilities=>cr_lf.

  LOOP AT lt_flights INTO DATA(ls_flight).
    lv_csv = lv_csv &&
             ls_flight-carrid  && ',' &&
             ls_flight-connid  && ',' &&
             ls_flight-price   && ',' &&
             ls_flight-currency &&
             cl_abap_char_utilities=>cr_lf.
  ENDLOOP.

  " Download to PC
  DATA lt_output TYPE STANDARD TABLE OF string.
  SPLIT lv_csv AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_output.

  cl_gui_frontend_services=>gui_download(
    EXPORTING
      filename = 'C:\temp\flights.csv'
      filetype = 'ASC'
    CHANGING
      data_tab = lt_output
    EXCEPTIONS
      OTHERS   = 1 ).
  IF sy-subrc = 0.
    MESSAGE 'CSV downloaded' TYPE 'S'.
  ENDIF.

Tab-delimited output

REPORT z_char_util_tab.

START-OF-SELECTION.

  SELECT carrid, connid, seatsmax, seatsocc
    FROM sflight
    INTO TABLE @DATA(lt_flights)
    UP TO 5 ROWS.

  DATA(lv_tab) = cl_abap_char_utilities=>horizontal_tab.

  LOOP AT lt_flights INTO DATA(ls_flight).
    DATA(lv_row) = |{ ls_flight-carrid }{ lv_tab }{ ls_flight-connid }{ lv_tab }{ ls_flight-seatsmax }{ lv_tab }{ ls_flight-seatsocc }|.
    WRITE: / lv_row.
  ENDLOOP.

CR_LF vs NEWLINE

Use CR_LF when generating files that will be opened in Windows Notepad or Excel — they require \r\n. Use NEWLINE for internal string processing, log entries, and email bodies. Mixing the two in one file produces inconsistent line endings.

See also

Comments