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,
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:
B.t.w., string templates |...| have type string and trailing blanks are preserved, of course. The logical expression ` ` = | | is true.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
3 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 |