Skip to Content
-3

&& operator adding trailing space

Aug 11, 2017 at 09:21 AM

108

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Horst Keller
Aug 11, 2017 at 09:57 AM
1

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 ' '.

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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 ?

0

"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.

1
Mike Pokraka Aug 11, 2017 at 10:01 AM
0

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
Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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 ?

0

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.

1