Skip to Content

To be honest, I feel a little bit uncomfortable writing this blog, because I think that all about this subject is said since long. But a recent discussion shows, that even experienced ABAPers can stumble over the handling of trailing blanks in character string processing from time to time. So why not summarize it. You can skip reading, if you know it all …

For character string processing ABAP provides mainly two built-in types, c and string. Text fields of type c are flat and have a fixed length, text strings of type string are deep and have a dynamic length. Besides that, there is another major difference between text fields of type c and text strings of type string:

While trailing blanks are always relevant in strings, they are ignored in text fields in many operand positions of statements, especially in source fields of assignments. As a rule: When working with text fields of type c you always should look into the ABAP Keyword documentation and check whether trailing blanks are skipped or kept in the respective statement.

Example:

    DATA: text_space   TYPE c LENGTH 1 VALUE ‘ ‘,

          string_space TYPE string VALUE ` `,

          result1      TYPE string,

          result2      TYPE string.

    result1 = ‘Word’ && text_space   && ‘Word’.

    result2 = ‘Word’ && string_space && ‘Word’.

The result of the concatenation when using text_space is „WordWord“ and the result when using  string_space is „Word Word“. The trailing blank – which is also the only character of text_space – is ignored in the text field. Be aware that the built-in constant space would have same behavior as text_space here!


And now watch out! The behavior regarding trailing blanks also concerns literals. We have two kinds of character literals in ABAP,


  • text field literals ‘…’ of type c
  • text string literals `…` of type string


It seems to be trivial, but one might tend to forget: What is said above about trailing blanks in c and string fields holds for the respective literals too. Especially text field literals ‘…’ can be rather nasty. Trailing blanks are not kept in many positions and that means that a text field literal containing one blank ‘ ‘ is often treated like an empty string. The problem is, it’s not WYSIWIG. You see a blank in the code but you don’t get it.


Examples:


DATA text TYPE string.

text = ‘ ‘.


text is an empty string of length 0.


DATA text TYPE string VALUE `blah_blah_blah`.

REPLACE ALL OCCURRENCES OF ‘_’ IN text  WITH ‘ ‘.


text contains “blahblahblah”.


IF ` ` =  ‘ ‘.

  BREAK-POINT.

ENDIF.


A running gag!


By the way, the concatenation operator && skips trailing blanks while the literal operator & keeps them:


DATA text TYPE string.

text = ‘Word ‘ && ‘Word’.

text = ‘Word ‘ ‘Word’.


The results are “WordWord” and “Word Word” respectively, oh my.


But that’s still not enough, there’s also another way around!


DATA text TYPE string.

CONCATENATE ‘Word’ ‘Word’ INTO text SEPARATED BY ”.

The result is “Word Word” with a blank! There is no empty text field literal in ABAP. It is always replaced by ‘ ‘. You don’t realize that in statements where trailing blanks are skipped. But behind SEPARATED BY they are kept!

Confused?

Rules of thumb:

  • Don’t use trailing blanks in text field literals ‘…’ of type c in operand positions where they are skipped
  • Always use text string literals `…` of type string, if you want to preserve trailing blanks in literals

B.t.w., string templates |…| have type string and trailing blanks are preserved, of course. The logical expression ` ` = | | is true.


To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

  1. Luciano Paiva

    Nice post!!

    A subject to remember:

    The preservation of trailing blanks can be forced for assignments by using the addition RESPECTING BLANKS of the statement CONCATENATE.

    The statement below generates a string with three blanks.

    CONCATENATE space ‘ ‘ INTO text SEPARATED BY ” RESPECTING BLANKS.

    Luciano

    (0) 
    1. Horst Keller Post author

      Yep, one way of assigning a text field of fixed length to a string preserving its trailing blanks could be

      CLEAR str.

      CONCATENATE str text INTO str

        RESPECTING BLANKS.

      Other tricks can also be found.


      B.t.w., the answer to the famous question how to fill a text string with one blank before string literals were invented was


      CONCATENATE ” ” INTO str

        SEPARATED BY ‘ ‘.

      (0) 
  2. Andrew Howell

    Not for the first time, I just had the surprise of literals having their trailing space removed.

    Would I be correct to say that I cannot create a constant string with trailing spaces?
    My program had

    CONSTANTS:
      c_separator TYPE string VALUE ', '
    .
    

    but of course the assignment is from text literal and the string ends up being length 1 with just the comma.

    I can make a data object and assign a string template:

    DATA:
      lv_separator TYPE string
    .
    lv_separator = |, |.

    but I don’t think string templates can be used to specify constant values? [I vaguely recall reading somewhere that they are doing something at runtime and I guess constants really are compile-time constants]

    (0) 
      1. Andrew Howell

        Thanks very much for the answer, I hadn’t realised “ were a thing in ABAP.

        I thought I’d covered everything to do with strings in the documentation but clearly not!

         

        Edit: In fact now I see them all over various posts above [blushing]

        I think my brain must have been quietly converting backquotes to single-quotes as I read…

        (0) 

Leave a Reply