cancel
Showing results for 
Search instead for 
Did you mean: 

Concatenating strings with trailing spaces

Former Member
0 Kudos

Hi,

can somebody advise me of how to concatenate strings so that trailing space is not ignored? For example I have two variabvles 'ABC ' and 'XYZ ' I need the result to be 'ABC XYZ '. By default trailing spaces are removed, how to retain them?

thanks,

erki

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member181879
Active Contributor

There is a much easier way.

CONCATENATE `abc ` 'xyz' INTO target.

Using the `` form will keep trailing space characters!

++bcm

DirkAltmann
Active Participant
0 Kudos

Hello Brian,

I have tried your code but it will not work. I got the followoing error message:

Field "`ABC" is unknown. It is neither in one of the specified tables

nor defined by a "DATA" statement.

I have used your code by copy and paste.

Best regards,

Dirk

Former Member
0 Kudos

Kirk,

Advise your R/3 version?

DirkAltmann
Active Participant
0 Kudos

Hi Francisco,

our Version is 3.1I.

Former Member
0 Kudos

The backquote ` is used to delimit a STRING literal whereas the single quote ' is used to delimit a text field literal.

The string literals retain any trailing spaces, the text literals do not.

But the STRING type was not available in 3.1I and therefore this method won't work for you :o(

Cheers,

John

Former Member
0 Kudos

You can also achieve the result you require with a deep data structure. Either group your elements in the order in which they should be concatenated or move the data into the elements into a separate structure. To access the concatenated result, use the higher level data name:

report z_jac_test.

data: begin of concat,

element1(4) value 'ABC ',

element2(4) value 'DEF ',

end of concat.

write: / '[' no-gap, concat no-gap, ']' no-gap.

athavanraja
Active Contributor
0 Kudos

you can use the following stt. also to achieve the same

data: test(100).

concatenate 'test' 'test1' 'test2' into test separated by ' '. (note that instead of using SPACE i have used ' ' and it works.)

Regards

Raja

DirkAltmann
Active Participant
0 Kudos

Hi Erki,

see the docu, this is the example from the docu:

Examples DATA: ONE(10) VALUE 'John',

TWO(3) VALUE 'F.',

THREE(10) VALUE 'Kennedy',

NAME(20).

CONCATENATE ONE TWO THREE INTO NAME SEPARATED BY SPACE.

Then, NAME has the value "John F. Kennedy".

Regards,

Dirk

Former Member
0 Kudos

I think Erki's point was that the result he is after, given your example is:


"John      F. Kennedy             "

The problem with the CONCATENATE statement, as Erki has discovered, is that it truncates trailing spaces. I think the easiest way to get around this problem is be replacing the space with a weirdo character (like a tab) that will not otherwise appear in the input.

Following code is untested and not unicode compliant.


DATA:
  BEGIN OF replacechars,
    space(1)       TYPE C,
    tab(1)         TYPE X VALUE '09',
  END OF replacechars.

DATA:
  BEGIN OF replacecharsreverse,
    tab(1)         TYPE X VALUE '09',
    space(1)       TYPE C,
  END OF replacecharsreverse.

TRANSLATE ONE USING   replacechars.
TRANSLATE TWO USING   replacechars.
TRANSLATE THREE USING replacechars.

CONCATENATE ONE TWO THREE INTO NAME.

TRANSLATE NAME USING replacecharsreverse.

Alternatively, in this situation, the length of the input field is most likely known, so the same could simply be achieved with (or slightly more complicated logic using dynamic offset specifications determined using the DESCRIBE statement):


NAME+0(10)  = ONE.
NAME+10(3)  = TWO.
NAME+13(20) = THREE.

Scott

DirkAltmann
Active Participant
0 Kudos

Hi Scott,

you are right. Erki, sorry for the misunderstanding.

Regards,

Dirk

Former Member
0 Kudos

Thank you very much, variable+0(10)=something is the thing that I was after. Somehow did not find it from documentation.

erki