ABAP String Templates New Feature in ABAP 731

By | July 11, 2013 | Concepts | 17,724 | 5

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:

ABAP String Templates Syntax

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?

Tags

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

5 Comments

  • Steve Oldner

    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!

  • Leandro

    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:

    Do not confuse the chaining operator && with the literal operator &, which concatenates two character literals as a literal. The literal operator is generally used if you want to define a literal string template across multiple program lines. The operator is executed only once, when the program is compiled and trailing blanks of character literals are always respected. A character string expression with a chaining operator, on the other hand, is recalculated each time (like all expressions) and can be used to concatenate any number of character-like operands.

    Thanks,
    Naimesh Patel

  • Leandro

    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

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.