String templates are ABAP expressions of result type string. You can use string templates in any reading operand position, for example, the right-hand side of a value assignment.
A string template begins and ends with a pipe-symbol ( | ). The simplest possible string template contains nothing but literal text. In this form a string template is not really different from a string literal.
What distinguishes a string template from a string literal is the ability to embed expressions. An embedded expression is an ABAP expression surrounded by a pair of curly brackets ( { and } ). At runtime, ABAP evaluates the embedded expression and translates the result into a string. In the final result, this string replaces the embedded expression (together with the surrounding curly brackets).
Note
ABAP syntax requires at least one blank after the opening bracket and at least one blank before the closing bracket.Of course one string template can contain more than one embedded expression.
Inside the curly brackets you can place any kind of ABAP expression: arithmetic expressions, like in the example above, but single variables or even literals can serve as embedded expressions.
One important use case for string templates is the controlled formatting of data for output.
In the first example, variable the_date is of type d and contains a date in the internal (raw) format YYYYMMDD (where YYYY stands for the year, MM for the two-digit month and DD for the two-digit date). When you use variable the_date as embedded expression in a string template the result will be the same as the internal format. But when you add format option DATE = <date_format> within the curly brackets, the system will format the value as a date. If you add DATE = ISO, the output will be in ISO format. With DATE = USER the output format depends on the user settings of the current user.
The second example illustrates some of the options you can use for formatting numbers. Using NUMBER you control the general formatting of numbers, for example, whether a decimal point is used or a decimal comma. Using SIGN you control the position of the sign and whether a plus sign (+) is displayed or not. Using STYLE you can choose from several pre-defined styles, like a scientific style or an engineering style.
You can join fields together using the concatenation operator &&. You can join any combination of data objects and string expressions.
The constituent parts of the expression are joined with no space or other separator between them. If you need spaces or another separator character, you must remember to insert it yourself as part of the expression as shown in the second example.