ABAP - String templates (Part 1)
The processing and creation of strings has been adjusted once more by SAP. The new structure of the strings is now easier and offers many advantages.
Table of contents
The topic of string processing has once again changed. The presentation and creation of a concatenated text is now a little more intuitive, as it is known from other programming language. The encoding of the strings looks a bit different, but here you will be able to get used to it quickly.
Current version
At the moment the string is shown with the "Accent grave", this can easily lead to problems with the recognition of the character string, because this can be confused with a simple quote. With the string, closing blanks are considered and not truncated as in the char field.
Here's a little example with an assignment and spaces at the end of the string. This is a simple assignment. The second example shows a combination of multiple strings joined together with a space in the middle.
DATA:
ld_value TYPE string.
" Creation of a string
ld_value = `This is a string. The spaces at the end still there ... `.
" with string connection
ld_value = `Part 1` && ` ` && `Part 2`.
New version
The new path now uses "pipes" that define the beginning and the end. In addition, the contents of variables can still be adopted with curly brackets. It is important to mention that every space between the pipes is a space at the end of the string.
DATA:
ld_value TYPE string.
" Creation of a string
ld_value = |This is a string. The spaces at the end still there ... |.
" with connection
ld_value = |Part 1| && | | && |Part 2|.
When using the curly braces, pay attention to the spaces between parentheses and variables, such as brackets with logical expressions or arithmetic operations. Any number of variables can be used in the string.
DATA:
ld_text TYPE char25 VALUE ''.
" Text and variable
DATA(ld_string) = |Here goes the variable: { ld_text }|.
Formatting
In the examples shown so far only the leading signs have been changed and it is still no great advantage to recognize the conventional strings. The advantage of the new variant is the possibility of formatting the variable when inserting it into the string.
There are various options available for preparing the variable contents for the output, which previously might have been implemented with additional intermediate variables or for which further function modules had to be used.
In order to format the content, there must be an addition after the variable, which determines the action to be performed.
Date
For easy output of the date in various formats the DATE function is available. The simplest form for this is the output in USER format, in most cases already the correct format for your needs.
" Date conversion
DATA(ld_date_out) = |{ sy-datum DATE = USER }|.
Alpha
The alpha conversion is the standard input and output form, which you need for all sorts of things to output and processing of numbers or document numbers needed. This allows leading zeros to be added or removed.
DATA:
ld_char TYPE CHAR10 VALUE '123'.
" Output
DATA(ld_belnr) = |{ ld_char ALPHA = IN }|.
Width & Align
Defines the length of a field for the output or for the length of the string that is generated. The string only can be extended, not shortened. With the formatting option Align, the text within the enlarged area can still be indented. In the following example, the text is positioned at the end of the string and padded with spaces in front of it.
" Output conversion
DATA(ld_width) = |{ 'Four' WIDTH = 10 ALIGN = RIGHT }|.
More
There are many more ways to convert output, you can find them under the link at the bottom of this article (sources).
Conclusion
The new string templates offer many new possibilities in the processing of messages, texts and outputs. The integrated formatting saves intermediate variables and makes the coding a lot clearer. In the next part we will deal with the new inline functions on string processing
Source:
SAP Documentation string templates
SAP Documentation string formatting