String Templates are introduced in SAP with ABAP release 731.
Introduction
ABAP string template is defined with two pipe sign symbols β|β. The text in between the two pipe sign is considered as the character string. The string template can only be used with the data objects of a type String.
The syntax looks like this:
The String template must start with a single pipe sign. It also must end with a pipe sign. A very basic example is:
Basic use of String Template
DATA result TYPE string. * result = |Hello world from Zevolving|.
This example has same effect like
result = 'Hello world from Zevolving'.
So, you may ask what is the difference β The different is in the power of the string templates. String templates can have expressions, characters strings and control characters within it. This would greatly reduce the need of the helper variables similar as method chaining.
Expressions within String templates
The expression or any mathematical formula can be included within the String template. To distinguish an expression from regular text, you would need to wrap the expression in the curly brackets { .. } . The expression within the brackets would be evaluated first and result would be than converted to character string.
To be able to understand it more, check out these code lines. It has Old Code using the helper lines and a new code using the String templates. Both providing the same results.
* --- OLD using Helper Variables DATA: lv_i TYPE i. DATA: lv_c TYPE char10. DATA: lv_string TYPE string. DO 5 TIMES. lv_i = sy-index * 10. lv_c = lv_i. CONDENSE lv_c. CONCATENATE lv_string 'Value' lv_c ',' INTO lv_string. ENDDO. WRITE: / lv_string. * --- NEW DATA: lv_string1 TYPE string. DO 5 TIMES. lv_string1 = lv_string1 && |Value| & |{ sy-index * 10 }| && ','. ENDDO. WRITE: / lv_string1.
Discussion
Do you think this would make your code more cleaner as less helper variables. Or you think this would make your code more complicated to read?
We are on BASIS 702 and have some of the new features as well. I really like the “one line” code.
Thanks for the useful blog!
What’s the difference between & and &&?
Hello Leandro,
Nice question π
&& is a chaining operator and is used to CONCATENATE the character literals. This gets checked at runtime and could have any number of literals to concatenate a string.
& is a literal operator. It would be used to define the string text across multiple program lines. It gets checked at compile time. Character literals joined using the literal operator & are also subject to the upper limit of 255 characters
There is good documentation in the ABAP Help of Character String Operator:
Thanks,
Naimesh Patel
Thanks a lot Naimesh. It’s because in your example everything is in the same line.
So it could be &&, right? I’m asking because the version that I use doesn’t have this functionality, so I can’t test.
Thanks in advance.
Hello Leandro,
I have create an article to show difference between && and &.
Hope it would help to clear remaining doubts π
Thanks,
Naimesh Patel