Skip to Content

Recently I want to get rid of specific character “S” from the ending of a text for example “ABSSS”, I thought ABAP command SHIFT text BY n PLACES RIGHT would work for me. I place it in a DO ENDDO loop to check whether the ending is “S”, then get rid of “S” by shifting the text. It worked only for the first occurrence, but no more, the code is here

REPORT  ZZTEST.
DATA text(5) TYPE  C VALUE ‘ABSSS’.
DATA textlen TYPE I.
DATA textlenm1 TYPE I. “to store STRLEn minus 1
DATA textlenac TYPE I. “to store STRLEN after CONDENSE
 
DO.

textlen = STRLEN( text ).
textlenm1 = textlen 1.
IF text+textlenm1(1) = ‘S’.
SHIFT text BY 1 PLACES RIGHT.
CONDENSE text. “to get rid of the leading space
  textlenac = STRLEN( text ).
WRITE: / ‘after condense, text now is’, text, ‘length is’, textlenac.
ELSE.
   write: / ‘ending is not S, exit, text now is’, text.
EXIT.
ENDIF.

ENDDO.

After I run it, It’s in the DO ENDDO loop forever. Why?

I debug it, after CONDENSE, the text is always “ABSS”. I realize that even after CONDENSE, STRLEN says it’s 4-character long, but system still remembers text is 5 character long, shifting it will not give “ABS” but ” ABSS” where there is a leading space. In this case, it’s hard to use SHIFT to realize my purpose, so I choose another way.

REPORT  ZZTEST.
DATA text(5) TYPE  C VALUE ‘ABSSS’.
DATA textlen TYPE I.
DATA textlenm1 TYPE I. “to store STRLEn minus 1
DATA textlenac TYPE I. “to store STRLEN after CONDENSE
 
DO.

textlen = STRLEN( text ).
textlenm1 = textlen 1.
IF text+textlenm1(1) = ‘S’.
*deleted following 2 lines

*SHIFT text BY 1 PLACES RIGHT.
*CONDENSE text. “to get rid of the leading space

*add a new line below

text = text+0(textlenm1).  “now it works for me, it will get ABSS, ABS and AB

textlenac = STRLEN( text ).
WRITE: / ‘after condense, text now is’, text, ‘length is’, textlenac.
ELSE.
   write: / ‘ending is not S, exit, text now is’, text.
EXIT.
ENDIF.

ENDDO.

Conclusion: STRLEN will ignore ending space, but SHIFT has a stubborn memory about how long the text is.

To report this post you need to login first.

3 Comments

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

  1. Alessandro Spadoni

    Hi Tony,

    we can use the power of the Regular Expressions where you faced this kind of problems,

    for example you could use this expression :


    REPLACE REGEX ‘[S\s]{1,}\Z’ IN text with space.

    replace the character “S” or the white space (\s) , occurring 1 or more times {1,} at the end of the string (\Z) 

    (0) 
  2. Jun Wu

    Number of characters in arg, where closing blanks in data objects with fixed lengths are not counted. They are counted though in data objects with the type string. If a non-Unicode double-byte code is used, a character that uses two bytes is counted twice. The argument can be byte-like outside Unicode programs.

    基本功要扎实

    (0) 

Leave a Reply