字符串模板生成式:
data lv_str type string.
lv_str = |Hello {sy-uname} this is SAP ABAPer world|
character_string = |This resulted in return code { sy–subrc }|.
More details please continue read below article.
What’s it al about?
Several new options for the handling of characters strings were introduced to ABAP in WAS702:
- String Templates: the option to create a character string out of literal texts, expressions, and control characters.
- Chaining Operator: chain two character-like operands into one new character string.
- Character String Functions: built-in functions for searching, processing and comparing strings.
I will first explain these options and give a few examples. The last paragraph explains how the use of string templates, combined with built-in functions and the use of functional methods, limits the number of needed ABAP statements.
Note that the ABAP Keyword documentation contains several built-in examples, related to old and new ABAP features. These examples can be executed and debugged and its code can be analysed. Moreover, the program DEMO_EXPRESSIONS offers 45 examples of character string and byte string processing.
String Templates
The purpose of a string template is to create a new character string out of literal texts and embedded expressions. It largely replaces the use of the WRITE TO statement, which will be explained later on.
A string template is defined by using the | (pipe) symbol at the beginning and end of a template.
DATA: character_string TYPE string.
character_string = |This is a literal text.|.
This example has in fact exactly the same result as:
character_string = `This is a literal text.`.
The added value of a string template becomes clear when combining literal texts with embedded expressions and control characters. Embedded expressions are defined within a string template with curly brackets { expression }. Note that a space between bracket and expression is obligatory.
An expression can be a data object (variable), a functional method, a predefined function or a calculation expression. Some examples are:
character_string = |{ a_numeric_variable }|.
character_string = |This resulted in return code { sy–subrc }|.
character_string = |The length of text element 001 ({ text–001 }) is { strlen(text–001 ) }|.
Embedded expressions have a default output format, but also several formatting options, comparable to the format options of the WRITE statement. Some examples are:
DATA: amount_field TYPE vbap–netwr VALUE ‘1234567.123’,
currency_field TYPE vbap–waerk.
character_string = |{ amount_field CURRENCY = currency_field NUMBER = USER }|.
character_string = |{ amount_field COUNTRY = ‘GB ‘ }|.
Two limitations for which I haven’t found a solution yet are:
1) I could not find a formatting option for Conversion Exits. Consider the example below: the WRITE….TO… calls the conversion exit KONPD that converts an internal project number to an external project id. The string template inserts the internal project number into the string.
DATA: project_number TYPE proj–pspnr.
SELECT SINGLE pspnr FROM proj INTO project_number .
WRITE project_number TO character_string2.
character_string = |{ project_number }|.
2) The following option for text-elements is not supported in an embedded expression:
WRITE : ‘This is a text’(001).
The only option to use a numbered text is in an embedded expression:
character_string = |{ text–001 }|.
However, using the Chaining Operator offers a simple solution for this as is demonstrated below.
Chaining Operator
The Chaining Operator && can be used to create one character string out of multiple other strings and string templates. The use of the chaining operator largely replaces the CONCATENATE statement.
In this example, a number text, a space, an existing character string and a new string template are concatenated into a new character string.
character_string = ‘Text literal’(002) && ` ` && character_string && |{ amount_field NUMBER = USER }|.
Built-in functions
SAP has added several new built-in functions for searching, comparing, and processing character strings. A few of these functions already existed before release WAS702, like for example, CHARLEN( ) or STRLEN( ). Other statements like FIND, REPLACE or TRANSLATE can now be replaced by built-in functions Also new functions have been added.
Note that these built-in functions can be used as part of string templates or in operand positions. In the ABAP keyword documentation, the added value of these functions is described as followed:
The string functions enable many string processing tasks to be done in operand positions where previously separate statements and auxiliary variables were required.
A few examples of built-in functions are:
string_result = to_mixed( val = string_field sep = ` ` ).
string_result = reverse( string_field ).
distance_between_strings = distance( val1 = string_field val2 = string_result ).
The functions to_mixed and reverse speak for themselves. The function distance is a similarity function that calculates the minimum number of steps needed to change one string into another string, the so called Levenshtein distance. I’m still looking for an opportunity to use it in one of my customer assignments