Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Character String and Byte String Processing →  Statements for Character String and Byte String Processing 

CONCATENATE

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


CONCATENATE {dobj1 dobj2 ...}|{LINES OF itab} 
            INTO result
            [IN {CHARACTER|BYTE} MODE]
            [SEPARATED BY sep]
            [RESPECTING BLANKS].

Extras

1. ... IN {CHARACTER|BYTE} MODE

2. ... SEPARATED BY sep

3. ... RESPECTING BLANKS

Effect

Concatenates either the content of the data objects dobj1, dobj2, ... of the rows of the internal table itab in accordance with their order and assigns them to the target field result. itab is a functional operand position. The following can be specified for the target field result:

  • An existing character-like or byte-like variable to which the result of the chaining can be converted.
  • An inline declaration with DATA(var). If IN CHARACTER MODE is used, the declared variable is of the type string; if IN BYTE MODE is used, it is of the type xstring.

If the target field result has a fixed length and this is greater than the length required, the field is filled on the right with blanks or hexadecimal 0. If the target field is too short, the concatenation is truncated on the right. If the target field is a string, its length is adjusted accordingly.

When character strings are processed, trailing blanks are usually ignored for data objects dobj1, dobj2 ... or rows in the internal table itab of fixed length.

System Fields

sy-subrc Meaning
0 The content of all data objects dobj1, dobj2 ... or itab rows was passed to the target field result.
4 The content of the data objects dobj1, dobj2 ... or itab rows could not be passed completely, since result is too short.


Notes

  • Instead of CONCATENATE, it is usually also possible to use string expressions for elementary fields. These expressions enable concatenations using either concatenation operators && or embedded expressions in string templates. The built-in function concat_lines_of can be used to concatenate rows in an internal table.
  • The ABAP runtime environment executes an internal optimization to reduce reallocations if new fragments are attached to an existing string within a loop. If the string or parts of the string themselves are appended, no optimization takes place. In loops this causes a squared increase in runtime. This can be prevented by using helper variables. See also the Performance Note for string expressions.

Example

The example shows that a chaining with CONCATENATE has the same result as a chaining with the chaining operator && or via embedded expressions.

CONCATENATE 'a' 'b' 'c' INTO DATA(str). 
ASSERT str = 'a' && 'b' && 'c'. 
ASSERT str = |{ 'a' }{ 'b' }{ 'c' }|. 

Addition 1

... IN {CHARACTER|BYTE} MODE

Effect

The optional addition IN {CHARACTER|BYTE} MODE determines whether character string or byte string processing is performed. If the addition is not specified, character string processing is performed. Depending on the processing type, the data objects dobj1, dobj2 ..., the rows of the internal table itab, and the separator sep must be character-like or byte-like.


Example

Chaining of individual bytes from an internal table in a byte string.

TYPES hex TYPE x LENGTH 1. 
DATA itab TYPE TABLE OF hex WITH EMPTY KEY. 

itab = VALUE #( 
  ( CONV hex( '48' ) ) 
  ( CONV hex( '65' ) ) 
  ( CONV hex( '6C' ) ) 
  ( CONV hex( '6C' ) ) 
  ( CONV hex( '6F' ) ) ). 

CONCATENATE LINES OF itab INTO DATA(xstr) IN BYTE MODE. 

cl_demo_output=>display( 
  cl_abap_conv_codepage=>create_in( )->convert( xstr ) ). 

Addition 2

... SEPARATED BY sep

Effect

The addition SEPARATED BY is used to insert the content of data object sep between the content of the consecutive data objects dobj1, dobj2 .... When strings are processed, trailing blanks are respected in separators sep of fixed length.


Example

After the first CONCATENATE statement, the result contains "Wehaveallthetimeintheworld", while after the second it contains "We have all the time in the world".

DATA: t1 TYPE c LENGTH 10 VALUE 'We', 
      t2 TYPE c LENGTH 10 VALUE 'have', 
      t3 TYPE c LENGTH 10 VALUE 'all', 
      t4 TYPE c LENGTH 10 VALUE 'the', 
      t5 TYPE c LENGTH 10 VALUE 'time', 
      t6 TYPE c LENGTH 10 VALUE 'in', 
      t7 TYPE c LENGTH 10 VALUE 'the', 
      t8 TYPE c LENGTH 10 VALUE 'world'. 

CONCATENATE t1 t2 t3 t4 t5 t6 t7 t8 
            INTO DATA(result). 
... 
CONCATENATE t1 t2 t3 t4 t5 t6 t7 t8 
            INTO result SEPARATED BY space. 

Addition 3

... RESPECTING BLANKS

Effect

The addition RESPECTING BLANKS is only allowed when strings are processed and respects the trailing blanks for data objects dobj1, dobj2 ... or rows in the internal table itab. If this addition is not used, the blanks are respected for data type string only.


Note

If the addition RESPECTING BLANKS is specified, the statement CONCATENATE can be used to assign any character strings text to target fields str of type string while respecting trailing blanks: CLEAR str. CONCATENATE str text INTO str RESPECTING BLANKS.


Example

After the first CONCATENATE statement, result contains "When_the_music_is_over", and after the second it contains "When______the_______music_____is________ over______". The underscores here represent blanks.

TYPES text   TYPE c LENGTH 10. 
DATA  itab   TYPE TABLE OF text. 

APPEND 'When'  TO itab. 
APPEND 'the'   TO itab. 
APPEND 'music' TO itab. 
APPEND 'is'    TO itab. 
APPEND 'over'  TO itab. 

CONCATENATE LINES OF itab INTO DATA(result) SEPARATED BY space. 
... 
CONCATENATE LINES OF itab INTO result RESPECTING BLANKS.