3
votes

On a SAP system, ABAP version 7.40 SP05, I just encountered a failure in unit tests on string comparison, but both strings should be the same?! Turns out it's not the case, as preceding conversion from i to string seems to produce extra trailing space in one of the strings.

This code bit:

DATA(i) = 111.
DATA(s1) = CONV string( i ).
DATA(s2) = '111'.
DATA(s3) = |111|.

Produces (as seen in debugger):

S1                                      111 3100310031002000    CString{4}
S2                                      111 310031003100    C(3)
S3                                      111 310031003100    CString{3}

The converted one has an extra trailing space. How does this happen and how can I prevent this to happen in i to string conversions? Obviously stuff like this makes me debug for a long time to find what is up (because unless I check the hex values, the debuger does not show that extra space...).

2

2 Answers

4
votes

To understand why the space is added in the first place, check the documentation on the default conversion rules that are applied by CONV:

The character "-" is set at the last position for a negative value, and a blank is set for a positive value.

Since you can't use the formatting options of string expressions with the CONV operator, I'd suggest changing the code to use |{ i }| (which might be a good idea for other values as well, since you'll probably need some formatting options when comparing date / time values in unit tests anyway).

1
votes

You cannot prevent it. The best way I found so far in ABAP is use CONDENSE s1

DATA i type i VALUE 12.
DATA idx TYPE string.
idx = i.       " idx = '12 '.
CONDENSE idx.  " idx = '12'.