Skip to Content
-3

&& operator adding trailing space

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Aug 11, 2017 at 09:57 AM

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

    Add comment
    10|10000 characters needed characters exceeded

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

  • Aug 11, 2017 at 10:01 AM

    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
    
    Add comment
    10|10000 characters needed characters exceeded

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