Replacing characters in a string is a simple programming task which should be ridiculously easy, right? It turns out that is not always the case, especially if you have to write SAP ABAP code. Let us assume that there is a variable with a base type of CHAR30 (a string of 30 characters) which needs to have its internal spaces replaced with hyphens. In theory the following statement should work.

DATA: lv_char30 TYPE char30.
lv_char30 = 'AAAA BBBB CCC'.
REPLACE ALL OCCURRENCES OF ' ' IN lv_char30 WITH '-'.

However, that code will generate a short dump and could even cause the SAP GUI to crash. If the SAP GUI does not exit then the REPLACE_INFINTE_LOOP runtime error will be shown as the cause of the short dump. According to the error analysis, SAP thinks that ‘ ‘ has a length of zero. Computer scientists are taught to start counting at zero, but that should not be the case when determining length. Using the constant SPACE does not change the result, it is still considered a zero length field. Performing the replace on STRING type variable does not eliminate the problem, nor does using IN CHARACTER MODE. The problem would also occur if the operation was performed in reverse (e.g. replace hyphens with spaces).

With the REPLACE statement being ruled out further solutions are less obvious. If the goal was to replace hyphens with spaces then using the OVERLAY statement may be attractive. However, that requires using another variable which is the same length as the target variable, but filled with spaces. If the length of the target variable grows then this approach would fail and likely produce mysterious results.

According to Sir Arthur Conan Doyle, Sherlock Holmes said, “When you eliminate all which is impossible, then whatever remains, however improbable, must be the truth.” So, we come to using the TRANSLATE statement, but even that is not straight forward. Here is an example of what one might expect to use.

DATA: lv_char30 TYPE char30.
lv_char30 = 'AAAA BBBB CCC'.
TRANSLATE lv_char30 USING ' -'.

However, the results are still not desirable.

AAAA-BBBB-CCC-----------------

ABAP assumes that the unused trailing memory addresses (after CCC up to the 30th character) contain spaces, so it helpfully converts those to hyphens as well. Unless that is actually what is desired, more steps will have to be taken to get rid of them. Since the STRING variable type has a variable number of characters, there are no trailing characters to perform the translation on. It is thus possible to use a STRING type variable to perform the TRANSLATE without getting trailing hyphens.

DATA: lv_char30 TYPE char30.
lv_char30 = 'AAAA BBBB CCC'. 

DATA: lv_string TYPE string.
lv_string = lv_char30.
TRANSLATE lv_string USING ' -'.
lv_char30 = lv_string. 

WRITE / lv_char30.

That is how, in four convoluted steps, ABAP forces programmers to replace spaces in a character string.

Comments (newest first)

Anonymous
TRANSLATE is within unicode environments not allowed.
Sara Lisa
Hay Chris awesome logic behind, lucky coming here. thanks and keep updating. SAP ABAP Online Training

Tags: Blogger SAP Code ABAP

Published: 2012-05-24