This is a test message to test the length of the message box.
ABAP Text, String, Template
Created by Software-Heroes

ABAP - Text, String, Template


How do I use character strings in modern ABAP development and how do they behave together with inline declarations. We also look at errors that can happen with it.

In the past, when defining and creating texts, you had to attach less importance to the correct filling, since the variables were defined at the beginning of the method and the type was already fixed. With the introduction of the inline declaration, however, this distinction is becoming increasingly important, since it can lead to erroneous behavior.



As a reminder, here is an example of how to create variables using inline declarations. There are plenty of other examples, here just to show how useful the statement is.

" Loop
LOOP AT lt_table INTO DATA(ls_structure).

" Result from method
DATA(lt_result) = get_some_data_for_me( ).

" Variables
DATA(ld_integer) = 5.


In the first example, we create a structure in the loop to process the data. In the second example, we get data from a method and bind the data locally into a variable. In the third example we create a variable by value. In the last example, the variable is very likely to be of the Integer data type, but the decision is up to the system.


Chars and Strings

Now we want to create a character string using an inline declaration and check which data type is created. To do this, we use the three common methods for character strings.

DATA(ld_var1) = 'Text'.
DATA(ld_var2) = `Text`.
DATA(ld_var3) = |Text|.


Now let's look at the information in the debugger and what data types are specified for it.


In the first case, a variable of data type CHAR is created that has exactly 4 digits, just like the text that is passed. In cases two and three, a 4-digit string is generated. As you can see, different ways produce different types of data.

How then should texts actually be generated? There is a chapter in Clean ABAP that deals with the topic. If you want to create a simple string literal, use `. If you build the text with variables, then you should use | use. Simply creating text with the pipe makes it rather illegible.



So what kind of errors can occur when using literals, you're probably wondering? Here we have an example for you:

  ld_pos TYPE i VALUE 3.

CASE ld_pos.
  WHEN 1.
    DATA(ld_symbol) = 'ONE'.
  WHEN 2.
    ld_symbol = 'TWO'.
  WHEN 3.
    ld_symbol = 'THREE'.
    ld_symbol = 'OTHERS'.


In this case, let's take a look at LD_SYMBOL in the debugger and what the current content of the variable is.


The variable contains the wrong value, or rather 2 characters are missing because the variable was defined as a CHAR with a length of 3 characters. The reason for this is the definition of the variable in the first CASE, where it is initialized with a 3-digit CHAR and thus the final type is determined. The definition takes effect at this point, even if it is not run through. According to Clean ABAP, such a variable should logically be defined at the beginning of the method and then it probably gets the appropriate type, since this must be specified.



The VALUE statement also makes it clear once again which data type it wants when filling. This example can also be applied to the INSERT. We have to use the right literal to fill the lines, otherwise the compiler will complain with an error message. Here is the corresponding example:


lt_char = VALUE #( ( 'AB' ) ( 'CD' ) ( 'EF' ) ).
lt_string = VALUE #( ( `GH` ) ( `IJ` ) ( `KL` ) ).



How do you get the right data type and what distinguishes the individual text types from each other, this should now be a little clearer with our article. When using it, you should be clear about what you need the literal for and use the appropriate type.

Included topics:
New ABAPCHARStringTemplate
Comments (0)

And further ...

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

ABAP - Type Casting

Category - ABAP

How do you actually get the original type of a class or instance if it is passed in a generic table? In this article we examine the possibilities.



Category - ABAP

After all these years, the “real” return in ABAP has finally arrived. In this article we will show you how it works and what it can do.


ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?


ABAP Deep Dive - Table access (internal)

Category - ABAP

In this article, let's take a look at table access to internal tables and how they replace READ TABLE.


ABAP Developer still relevant

Category - ABAP

In this article we look at whether ChatGPT can already replace an ABAP developer or whether it can be used as a help in everyday life.