15

Is there another way to concatenate in ABAP instead of using the CONCATENATE keyword?

An example using CONCATENATE:

DATA:
  foo    TYPE string,
  bar    TYPE string,
  foobar TYPE string.

  foo = 'foo'.
  bar = 'bar'.

  CONCATENATE foo 'and' bar INTO foobar SEPARATED BY space.
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Eduardo Copat
  • 4,941
  • 5
  • 23
  • 40

5 Answers5

32

You can (starting with ABAP 7.02) use && to concatenate two strings.

Data:
foo    TYPE string,
bar    TYPE string,
foobar TYPE string.

foo = 'foo'.
bar = 'bar'.

foobar = foo && bar.

This also works with character literals:

foobar = 'foo' && 'bar'.

For preserving spaces, use this kind of character literal named "text string literal" which is defined with two grave accents (U+0060):

foobar = foo && ` and ` && bar
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
oldwired
  • 527
  • 5
  • 12
  • That works great too! But you can't concatenate with spaces with this operator, try this: `foobar = foo && ' and ' && bar`. – Eduardo Copat Sep 18 '13 at 12:48
  • 4
    Try with foobar = foo && ` and ` && bar. With the inverted apostrophe (Don't know the name of that symbol. – Nelson Miranda Sep 19 '13 at 23:20
  • @nmiranda apparently it's called a back quote or a grave quote (among other things. see here http://www.computerhope.com/jargon/b/backquot.htm and it's used for specifying string literals. The thing with strings is that the spaces at the end are not ignored, as they are when concatenating characters (or character literals - defined with single quotes) – vlad-ardelean Sep 21 '13 at 22:36
  • @nmiranda I added your variant to the post. – oldwired Sep 25 '13 at 19:14
16

Yes, you can use String Templates, which were introduced in ABAP 7.02.

An example following:

DATA:
  foo    TYPE string,
  bar    TYPE string,
  foobar TYPE string.

  foo = 'foo'.
  bar = 'bar'.

  foobar = |{ foo } and { bar }|.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Eduardo Copat
  • 4,941
  • 5
  • 23
  • 40
6

Besides the String Expressions mentioned by Eduardo Copat, it is sometimes sensible to use the MESSAGE ... INTO ... statement - especially if the text is supposed to be translated. In some translations, the positions of variables relative to each other have to be swapped, and it is generally much easier to translate the text You cannont combine &1 with &2. than the separate parts You cannot combine and with.

vwegert
  • 18,371
  • 3
  • 37
  • 55
2

You can use && or |{}{}| notations.

You don't need to type and between the objects, if you give space it will take it as space or any other.

"no space:
foobar = |{ foo }{ bar }|.
"1 space:
foobar = |{ foo } { bar }|.

etc.

Oguz
  • 1,867
  • 1
  • 17
  • 24
-2
DATA: v_line TYPE string.


CONCATENATE 'LINE1' 'LINE2' 'using cl_abap_char_utilities=>NEWLINE' INTO v_line SEPARATED BY cl_abap_char_utilities=>NEWLINE.

CALL FUNCTION 'LXE_COMMON_POPUP_STRING'
  EXPORTING
    text          = v_line
          .

CLEAR: v_line.

CONCATENATE 'LINE3' 'LINE4'  'cl_abap_char_utilities=>HORIZONTAL_TAB' INTO v_line SEPARATED BY cl_abap_char_utilities=>HORIZONTAL_TAB.

CALL FUNCTION 'LXE_COMMON_POPUP_STRING'
  EXPORTING
    text          = v_line
          .

CLEAR: v_line.

CONCATENATE 'LINE5' 'LINE6'  'cl_abap_char_utilities=>VERTICAL_TAB' INTO v_line SEPARATED BY cl_abap_char_utilities=>VERTICAL_TAB.

CALL FUNCTION 'LXE_COMMON_POPUP_STRING'
  EXPORTING
    text          = v_line
          .


CLEAR: v_line.

CONCATENATE 'LINE7' 'LINE8'  'cl_abap_char_utilities=>CR_LF' INTO v_line SEPARATED BY cl_abap_char_utilities=>CR_LF.

CALL FUNCTION 'LXE_COMMON_POPUP_STRING'
  EXPORTING
    text          = v_line
          .
sid
  • 1