ABAP Chaining Operator && and how to use it

I am not particularly fond of the excess of the ABAP statements to achieve simple tasks and really like the new adaption to operators and predefined functions to achieve similar or sometimes better results. In line with that, this post is about my favourite, chaining operator or &&.

For folks who have never seen this or want to know more about it,

What is a Chaining Operator?

The chaining operator concatenates two operands in a string expression as a character string. It has the ability to allow string expressions, predefined functions or functional methods, and even method chaining at its operand position. 
To Me, It's an upgrade over the good old CONCATENATE statement.

Syntax: … operand1 && operand2 … .

Below are a few examples of Chaining Operator and its usage.

Simple Concatenation: 
REPORT sy-repid.
DATA:
lv_string TYPE string.
START-OF-SELECTION.
"Using Concatanate
CONCATENATE `Test`
`Concatenate`
INTO lv_String.
write:/ lv_string.
"Using Chaining Operator
Lv_string = `Test` && `Chaining Operator`.
write:/ lv_string.

Adding Separator:

REPORT sy-repid.
DATA:
lv_string TYPE string.
START-OF-SELECTION.
"Using Concatanate
CONCATENATE `Test`
`Concatenate`
INTO lv_String
SEPARATED BY space.
write:/ lv_string.
"Using Chaining Operator
Lv_string = `Test` && ` ` && `Chaining Operator`.
write:/ lv_string.

Adding Different Separators in a text:
REPORT sy-repid.
DATA:
lv_string TYPE string,
lv_string2 TYPE string.
START-OF-SELECTION.
"Using Chaining Operator
lv_string = to_upper(`Test`) && ` ` && to_upper(`Chaining Operator`)
&& `:` && ` ` && `with space and colon`.
WRITE:/ lv_string.
"Using Concatanate
CONCATENATE `Test`
`Concatenate`
INTO lv_string
SEPARATED BY space.
lv_string = to_upper( lv_string )."COnvert the case to Upper
CONCATENATE space
`with space and Colon`
INTO lv_string2
RESPECTING BLANKS .
CONCATENATE lv_string
lv_string2
INTO lv_string
SEPARATED BY ':' .
WRITE:/ lv_string.

Notice how effortlessly different separators can be embedded in the string.

Using predefined functions:
REPORT sy-repid.
DATA:
lv_string TYPE string,
lv_string2 TYPE string.
START-OF-SELECTION.
"Using Chaining Operator
lv_string = to_upper(`Test`) && ` ` && to_upper(`Chaining Operator`)
&& `:` && ` ` && `with space and colon`.
WRITE:/ lv_string.
"Using Concatanate
CONCATENATE `Test`
`Concatenate`
INTO lv_string
SEPARATED BY space.
lv_string = to_upper( lv_string )."COnvert the case to Upper
CONCATENATE space
`with space and Colon`
INTO lv_string2
RESPECTING BLANKS .
CONCATENATE lv_string
lv_string2
INTO lv_string
SEPARATED BY ':' .
WRITE:/ lv_string.

Dynamic text building Using the functional method and method chaining:
REPORT sy-repid.
DATA:
lv_string TYPE string,
lv_string2 TYPE string.
PARAMETERS:
p_txt TYPE char4.
*----------------------------------------------------------------------*
* CLASS lcl_main DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_main DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
ret_string IMPORTING iv_num TYPE i
RETURNING value(rv_string) TYPE string.
ENDCLASS. "lcl_main DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_main IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_main IMPLEMENTATION.
METHOD ret_string.
CASE iv_num.
WHEN 1.
rv_string = `One`.
WHEN 2.
rv_string = `Two`.
WHEN 3.
rv_string = `Three`.
WHEN OTHERS.
rv_string = `Not Supported`.
ENDCASE.
ENDMETHOD. "ret_string
ENDCLASS. "lcl_main IMPLEMENTATION
START-OF-SELECTION.
"Count and display the number of characters in input
"Using Chaining Operator
lv_string = `Length of the input text is:` && lcl_main=>ret_string( numofchar( p_txt ) ).
WRITE:/ lv_string.
"Using Concatanate
lv_string2 = lcl_main=>ret_string( numofchar( p_txt ) ).
CONCATENATE `Length of the input text is:`
lv_string2
INTO lv_string
SEPARATED BY space .
WRITE:/ lv_string.

You can also pass the string template to a chaining operator operand position:
REPORT sy-repid.
DATA:
lv_string TYPE string.
START-OF-SELECTION.
lv_string = |You can connect | && |two or more | && |string templates |
&& |across mupliple lines.|.
WRITE: lv_string.

Above are a few examples of how you can use the chaining operator. As always, Hope you like the post and it helps you in your day-to-day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP