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.

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Suhas Saha

    Hi Horst,

     

    I did not think using the string templates – |\n|, |\r|, |\t| – with the REPLACE, FIND, SPLIT constructs Thanks for highlighting these.

     

    BR,

    Suhas

    (0) 
  2. Nikolay Evstigneev

    Hi, Horst!

    Nice post!

    As Suhas said, commands REPLACE, FIND, etc. will definitely win from that.

    I believe ABAP to be a bit “wordy” language, so any template to produce less coding should be given a standing ovation

    (0) 
  3. Ankit Sharma

    Hi Horst,

     

    Thanks a lot for sharing your ideas and views.

     

    I am just trying to find out some thing related to CR and LF, it is some thing I am stuck with these days, may be your further sugessitions will help me.

     

    The problem is that I am creating a file over Application server,

    here I am using K/W TRANSFER, transfer puts a default LF at the end of each line.

     

    But my requirement is a CRLF at the end of each line, when I am using CRLF from ABAP Class then it creates LF + CRLF at the end of a record.

     

    Actually the file created by me is processed by Java System.

     

    So, I was just wondering, how to achieve that.

     

    May be your views can help me.

     

    Thanking You All.

    (0) 
    1. Horst Keller Post author

      Hello,

       

      You can find in the documentation for TRANSFER that when writing to text files, automatically a line end marker is added to the data. You can suppress that with the addition NO END OF LINE. Which line end marker is added can be defined when opening the file using the addition WITH LINEFEED. Otherwise it is implicitly set and does not always fit.

       

      If the WITH LINEFEED addition doesn’t help convert your text into binary data using CL_ABAP_CODEPAGE specifying the required codepage, add the required LF to the end of each line and write to files opened in binary mode. As for XML- or JSON-data it is a recommended way to work with binary data also when working with files since this makes you independent from such things as implicitly added LFs or unknown characters.

       

      Best

      Horst

      (0) 

Leave a Reply