Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

&& operator adding trailing space

Former Member
0 Likes
36,498

Hi All,

I have used the below chaining operator to prevent the truncation of trailing space while concatenating two variables. It worked for the first part that is it didn't truncate the trailing space after 'Sun ' in the below code snippet as expected but for the second output where there was no trailing space after 'Sun' it actually added a space in the final output which was not expected.

Can you please suggest , how to resolve this?

Data : lv_str1 type string,

lv_str2 type string.

lv_str1 = 'The Sun '. " Space after Sun
lv_str2 = 'is shining'.
WRITE:/ |{ lv_str1 } | && |{ lv_str2 }|.

lv_str1 = 'The Sun'. " No space after Sun
lv_str2 = 'is shining'.
WRITE:/ |{ lv_str1 } | && |{ lv_str2 }|.

Output :

The Sun is shining
The Sun is shining

6 REPLIES 6
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
15,836

WRITE:/ |{ lv_str1 } | && |{ lv_str2 }|.

There is a literal blank character between } and |.

This is leading to the space in both cases, not the && !!!

Trailing spaces are preserved in string fields and string literals ` ` and in string templates | | and they are cut in text fields and text field literals ' '.

Read only

0 Likes
15,836

Sorry I added a literal blank between } and |.That explains the space added between the string.

But How do I use `` with variables? The texts that I'm dealing with are dynamic , so I cannot do something like

lv_str1 = `The Sun is `. lv_str for me has texts which are dynamic. How do I handle it ?

Read only

15,836

"But How do I use `` with variables?"

This question makes no sense.

Please make yourself knowledgeable about the concept of literals and variables, about data types in ABAP, and about character processing in ABAP first.

Read only

pokrakam
Active Contributor
15,836

Ugh... you're mixing techniques and data types in a way that's asking for trouble.

CONCATENATE or the && operator will remove spaces from CHAR-types but not from strings. String templates return strings.

Your example puts a char into a string and combines them using a char-type operator. The space you see is the one from your string template (it's easier to see if you post using code formatting!).

Either: Use string templates throughout:

write: / |{ str1 } {str2}|.  "Space

Or do concatenation:

write: / str1 && str2.   "No space here

or use strings as your data type:

data(str1) = `The sun `.
data(str2) = `is shining`.
write: / str1 && str2.  "Space
write: / |{ str1 } { str2 }|.  "Two spaces
Read only

Former Member
0 Likes
15,836

How do I use `` with variables? The texts that I'm dealing with are dynamic , so I cannot do something like

lv_str1 = `The Sun is `. lv_str for me has texts which are dynamic. How do I handle it ?

Read only

pokrakam
Active Contributor
15,836

You handle it the same way as we have been doing with other data type conversions (numc <-> i, dec <-> float, etc.): Just be aware of what data types you're using and their behaviour and limitations.

A string has a content length, C doesn't. Therefore during conversion, the length of a C is determined by the last non-blank character. Use/declare appropriate data types. Convert if you have to, using the conv operator. String templates convert everything to strings before performing their operations, CONCATENATE to char.

Sorry I don't know what else to add unless you can give me a concrete example.