Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert

What is a line feed?

A line feed is a control character as defined in Wikipedia. In former times such control characters were used to mechanically feed a line in a teletype writer or to let the carriage literally return to the left of the paper. Nowadays, such control characters are mainly used to position cursors in outputs. Many output programs interpret the most important control characters for line feed, return, and tabulator according to their meaning. In source code, HTML or XML files, such control characters are handled as whitespace characters. They are not interpreted as part of the file but can help to make it more readable.

How to get a line feed character in ABAP?

If you search the Web for how to get a line feed character or another control character in ABAP, you mainly find the class CL_ABAP_CHAR_UTILITIES. And in fact, you can use its attribute CL_ABAP_CHAR_UTILITIES=>NEWLINE for that. By but why so complicated? Since Release 7.02, the string templates that are enclosed in |-delimiters, allow you to denote a line feed directly.

|....\n...|  for a linefeed among other contents or |\n| for a single line feed character.

Other control characters supported by string templates are \r and \t for returns and tabs If you only need those, no need to use CL_ABAP_CHAR_UTILITIES. To prove that, run the following lines of code in your system:

ASSERT cl_abap_char_utilities=>newline        = |\n|.
ASSERT cl_abap_char_utilities=>horizontal_tab = |\t|.
ASSERT cl_abap_char_utilities=>cr_lf          = |\r\n|.

Both, the attributes of CL_ABAP_CHAR_UTILITIES and the special characters of the string templates return control characters according to the code page of the ABAP application server. You can find out the code for the control character as follows (or simpler in the ABAP Debugger):

FIELD-SYMBOLS <lf> TYPE x.
DATA lf type c LENGTH 1.
lf |\n|.

ASSIGN lf to <lf> casting.
cl_demo_output=>display( <lf> ).


In an Unicode system this gives 0A00. This is the code for a line feed as it is shown in all encoding tables in the Web, surprise,surprise.

Of course, this works also the other way arond. If you need a control character for another code page, you look it up and do something like

CONSTANTS lf TYPE x LENGTH 4 VALUE '0A00'.
FIELD-SYMBOLS <lf> TYPE c.

ASSIGN lf TO <lf> CASTING.
cl_demo_output=>display( |aaa{ <lf> }bbb| ).


The output is

aaa

bbb

amazing, that leads us to the next section.

Where to use a line feed character in ABAP

First, where  to use it not. You cannot use a line feed character or any other control character in classical list programming. The following line


WRITE |aaaa\nbbbbb|.


produces an output like aaaa#bbbbb. The classical list processor does not recognize the control character and it does not treat it as a whitespace. It is an unknown character that is displayed as #. In classical list programming you have to use two (chained) WRITE-Statements:

WRITE: |aaaa|, / |bbbbb|.


Now where to use line feeds and other control characters? You use them, if you want to send them somewhere, where they are understood. E.g., writing to a file or sending to other displays than classical lists:

cl_demo_text=>show_string(
  |<html>| &&
  |  <body>| &&
  |    Hello!| &&
  |  </body>| &&
  |</html>| ).

cl_demo_text=>show_string(
  |<html>\n| &&
  |  <body>\n| &&
  |    Hello!\n| &&
  |  </body>\n| &&
  |</html>\n| ).

While the first output gives

<html>  <body>    Hello!  </body></html>.

The second gives

<html>

  <body>

    Hello!

  </body>

</html>

By the way

cl_abap_browser=>show_html(
  EXPORTING html_string =
    |<html>\n| &&
    |  <body>\n| &&
    |    Hello!\n| &&
    |  </body>\n| &&
    |</html>\n| ).

gives the exepected output, the browser ignores the line feeds.

Now for writing to a file:

TYPES text TYPE TABLE OF string WITH EMPTY KEY.

DATA(text) = VALUE text( (
  |<html>\r\n| &&
  |  <body>\r\n| &&
  |    Hello!\r\n| &&
  |  </body>\r\n| &&
  |</html>\r\n| ) ).

cl_gui_frontend_services=>gui_download(
  EXPORTING filename = 'c:\temp\text.htm'
  CHANGING  data_tab = text  ).

If you open the file with the notepad editor, you will see the line breaks. Note that I have used \r\n here, since \n is not sufficient in Windows. Note also that APIs for writing or reading files as OPEN DATASET or the above CL_GUI_FRONTEND_SERVICES also offer capabilities to handle control characters.

Last but not least, if you receive data from somewhere and you want to get rid of control characters, you can of course do replacements like this

REPLACE ALL OCCURRENCES OF |\n| IN text_with_lf WITH ``.

I also like this one:

SPLIT text_with_crlf AT |\r\n| INTO TABLE DATA(text).

But don't forget to check if the code pages match. If the code page of your AS ABAP does not match the code page of the control characters in the text, neither string templates nor CL_ABAP_CHAR_UTILITIES do help. Then you must construct a field symbol <lf> as shown above.

That's already all, I wanted to say about that.

7 Comments