Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Character String and Byte String Processing →  Expressions and Functions for String Processing →  string_exp - String Expressions 

string_exp - String Templates

Other versions: 7.31 | 7.40 | 7.54

Syntax


|[literal_text][
embedded_expressions][
control_characters]|

Effect

A string template is enclosed by two characters "|" and creates a character string that is used by the string expression instead of the string templates. The characters of this character string consist of any sequence of the following syntax elements of the string template:

A string template that starts with "|" must be closed with "|" within the same line of source code. The only exceptions to this rule are line breaks in embedded expressions. There are, however, no length restrictions on a string template. The literal operator & or the chaining operator && can be used to join multiple string templates in a single string template. A string template can be defined across multiple lines of source code and be given comments.


Notes

  • To display the delimiters "|" in literal text in a string template, you must prefix them with the escape symbol \.

  • The delimiter characters "|” can be formatted in the ABAP Editor by choosing Fonts and Colors → Token operator to highlight them in the source code.

  • Using the character string function escape, all special characters for character string templates can be put in front of their escape character.

  • When string templates are joined, the literal operator & behaves differently than with literals. This operator is executed at runtime (like the chaining operator &&) and not during compilation as a one-off process. This means the restriction of 255 characters for literals no longer applies. The two joins
|...| &  |...|
|...| && |...|
are identical for string templates. For literal-only content, they are the same as
... && ...
However they are different to
... &  ...
'...' &  '...'
'...' && '...'
In the first two cases, a length restriction of 255 characters applies. In the third case, trailing blanks are ignored.

  • If the right side of an assignment appends strings using string templates to a variable specified with the type string on the left side of the assignment, the variable is used directly in some cases without producing a subtotal. Care must be taken to preserve this optimization, especially in loops.

Example

String template with literal text and an embedded expression. The result is made up of the content of the literal text and the result of the embedded expression.

DATA(result) = |Hello { sy-uname }!|.

Example

The following piece of source code shows three similar string templates that all display the character string "Hello World!". The first string template contains the entire character string as literal text. The next two string expressions distribute the literal text across multiple parts of the string template. The literal operator & is used to join them as the first string template.

DATA(result1) = |Hello World!|. 

DATA(result2) = |Hello| & | | & |World| & |!|. 

DATA(result3) = |Hello| & "sub template 1 
                | |     & 
                |World| & "sub template 3 
* sub template 4: 
                |!|.

Continue

String Templates - literal_text

String Templates - embedded_expressions

String Templates - control_characters

Examples of string templates