ABAP Characters , Strings and Traps of Trailing Blanks

No matter how much I try to move away, I keep coming back to this. I have talked about this multiple times in multiple posts and yet it seems less. So bear with me and hope you stay sane/awake by the time we reach the end.

Alright, The issue is simple. Handling of trailing blanks. No need to read further if you know it all. For the rest of the mortals like me, let's proceed.

When it comes to character string processing, ABAP gives you two built-in options: 

C - Fixed length, trailing blanks are ignored, minimum length is 1.

STRING - Dynamic length, trailing blanks are relevant, minimum length is 0.

Before you start feeling unrewarded for all the build-up at the start, I have a program to share:

Running the program gives this output.
Character Literal Trailing Blank issue
The Output is different even with the seemingly same code. 
Why? Because the devil is in the details,
c_text_space is type c and contains one blank(trailing) which gets ignored. It seems trivial but is very easy to miss. The problem is that you see a blank in the code but you don't get it with text literal. 

See, easy peasy, lemon squeezy, just that lemon gets replaced with your brain sometimes.
Run this code and see how:
Here is the output:
Concatenate using string/Char blank
The Output has reversed. But Why? because the ABAP gods have decided so.
There are no empty text field literals in ABAP, their minimum length is 1, so they internally get converted to ' '(containing blank of length 1), and Separated by clause respect the blanks.

Whereas, the text string field stays as ``(no space in between) because the minimum length is 0. Hence the output is without space.

Alright, let's go a step further. What do you think is the output of this code? 
Here is the output:
String length on Text field/String literal
String length should have been 1 in all cases as string can hold blanks, but it's not. 
It's the same culprit, text field literal, whose trailing blank is ignored and thus an empty character is assigned to the string in the first case, which leaves the string empty with zero length.

Congratulations for making it this far. We are just about to end it with one more example.

Suppose you want to replace a part of your text with space. Say "X,Y,Z" -> "X Y Z".
You go ahead and use your trusty old replace statement. here is the code:
If the output is what you expected, the whole post was worth it. Otherwise, take a break and join me at the beginning of the post some other time.

In Closing:

  It can get ugly very fast when using type strings and c interchangeably, always follow the rule of thumb: 
  - Do not have trailing blanks in text field literals or type c variables.
  - Use text string literal to preserve trailing blanks.
 More on Literals can be found on the post Text field literal vs string field literal.

 Another lifesaver is String templates, their return type is string and spaces are always preserved, doesn't matter the position. Plus, they can do much more than just hold literal.
Check out the post String Processing using String Templates and String templates examples to get more understanding of them.
As always, Hope you liked the post and it helps you in your day to day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Application Logging in ABAP