This is a test message to test the length of the message box.
Login
|
New ABAP string templates
Created by Software-Heroes

ABAP - String templates (Part 1)

5140

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.

Advertising


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


Included topics:
New ABAPString Template|{ }|
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Tuesday and Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP - XCO Logging

Category - ABAP

The XCO classes are part of the ABAP Cloud APIs and offer numerous functions that aren't always easy to understand. In this article, we'll take a detailed look at the logging object.

12/16/2025

ABAP - The right Key

Category - ABAP

What about the use of internal tables? Is it still just TYPE TABLE in ABAP, and the table is fully defined?

11/14/2025

ABAP - XCO Regular Expressions

Category - ABAP

Let's take a look at the XCO classes for regular expressions and how you can easily use them to execute REGEX against text and input in ABAP Cloud. We'll also compare them with classic ABAP.

11/07/2025

ABAP - Escape

Category - ABAP

In this article, let's take a closer look at different escape variants that you need for ABAP development and system security.

10/07/2025

ABAP - Date and Time

Category - ABAP

In this article, let's take a closer look at the data types for dates and times in ABAP. Have any changes been made between the various releases, and what should you still use today?

10/03/2025