SAP LINE FEED



Get Example source ABAP code based on a different SAP table
  



Line Feeds in Character String Processing
A line feed (LF) is a control character that positions the cursor when writing an output. Other of such control characters are return (CR from carriage return) or a tabulator. There are also combinations possible as CRLF for return plus line feed. If a character string contains control characters, it depends on the framework handling the character string how these are interpreted. While, for example, most notepad applications or text editors handle control characters as such, HTML or XML browsers handle control characters as whitespace characters.
ITOC

Control Characters in ABAP
In ABAP, some control characters, especially the line feed character, can easily obtained by using the respective string template:
|... n...| for a linefeed among other content
| n| for a single line feed character
There are also constants for control characters in CL_ABAP_CHAR_UTILITIES but those contain the same code as the respective string templates and there is no need to use them any more: ASSERT cl_abap_char_utilities=>newline = | n|.
ASSERT cl_abap_char_utilities=>horizontal_tab = | t|.
ASSERT cl_abap_char_utilities=>cr_lf = | r n|.

ABAP_EXAMPLE_VX5
The actual code of a control character can be determined (as for all characters) as follows:
ABEXA 01762
In an Unicode system this gives 0A00 which is the Unicode code for a line feed.
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
In another way around, any control character of any code page can be included in a character string, here again the Unicode line feed:
ABEXA 01763
ABAP_EXAMPLE_END

Using the Line Feed Character in ABAP
The line feed character and other control characters are needed when sending character strings to frameworks that can handle them. Examples are different output media or writing to files.

Line Feed in Outputs
The following are examples for line feeds in common output media.

ABAP_EXAMPLE_VX5
The text control of the SAP GUI Control Framework handles control characters.
ABEXA 01764
The first output is: <(> Hello! <)>
The second output is: <(><)>
<(> <)>
<(> Hello!<)>
<(> <)>
<(><)>
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
The first output of string handles its content as text and the line feeds are respected. The second output of string browses its content as an HTML file and the line feeds are handled as whitespace characters.
ABEXA 01765
ABAP_EXAMPLE_END
BEGIN_SECTION VERSION 5 OUT

Line Feed in Files
The following is an example for line feeds written to a file.

ABAP_EXAMPLE_VX
Writing text to a text file of the frontend computer. TYPES text TYPE TABLE OF string WITH EMPTY KEY.

DATA(text) = VALUE text( (
<(> | r n| <)>
<(> | r n| <)>
<(> | Hello! r n| <)>
<(> | r n| <)>
<(> | r n| ) ).<)>

cl_gui_frontend_services=>gui_download(
EXPORTING filename = 'c: temp text.htm'
CHANGING data_tab = text ).
Opening the file with a notepad editor shows that the line feeds are handled there. Here, r n is used, since n is not sufficient in Windows. The ABAP File Interface or the above used class CL_GUI_FRONTEND_SERVICES offer further capabilities to handle control characters.
ABAP_EXAMPLE_END

Line Feed in Classic Lists
The line feed character and other control characters cannot be used in classic lists. The list processor does not recognize a control character and it does not handle it as a whitespace. It is an unknown character that is displayed as #. In classic lists, two (chained) WRITE statements are needed: WRITE: |aaaa|, / |bbbbb|.
END_SECTION VERSION 5 OUT

Replacing Line Feeds
It is a common task to remove line feeds and other control characters from character strings that are received from outside of ABAP.
This can be easily done as follows: REPLACE ALL OCCURRENCES OF | n| IN text_with_lf WITH ` `.
Another pattern is SPLIT text AT | r n| INTO TABLE DATA(text_table).
For doing so, it must be checked If the code pages match. If the code page of the AS ABAP does not match the code page of the control characters in the text, field symbols must be used as shown above to represent the control characters of the code page of the text.