Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SHIFT statement doesn't work properly

SebastianK
Explorer
0 Kudos

Hi,

so I have one simple problem which I don't understand:

DATA s TYPE string.

s = 'KEY_KOSTL'.

SHIFT s LEFT DELETING LEADING 'KEY_'.

My expectation is that "s" has now the content 'KOSTL' but actually it's 'OSTL'. It also doesn't matter if the value is 'KEY_KKKOSTL', 'KEY__KOSTL' or 'KEY_KEOSTL', the result is always 'OSTL'.

How does that SHIFT really work? Any ideas?

Cheers

11 REPLIES 11

Former Member
0 Kudos

This message was moderated.

Former Member
0 Kudos

Try this way

DATA: lv_word   TYPE string,

       lv_delete TYPE string,

       lv_lenght TYPE i.

lv_word = 'KEY_KOSTL'.

lv_delete = 'KEY_'.

lv_lenght = strlen( lv_delete ).

SHIFT lv_word BY lv_lenght PLACES LEFT.

Sougata
Active Contributor
0 Kudos

Sebastian,

How SHIFT works is well documented in transaction ABAPHELP including all its additional keywords.

You could also use Shift Functions instead and have it in 1 line of source code.

s = shift_left( val = s places = 4 ).

"OR

s = shift_left( val = s sub = s(4) ).

"OR

s = shift_left( val = s sub = |KEY_| ).


Cheers,

Sougata.

former_member209120
Active Contributor
0 Kudos

Hi Sebastian Kotsch,

Try like this

TYPES : BEGIN OF ty_s,
         s TYPE string,
         END OF ty_s.

DATA : it_s TYPE TABLE OF ty_s,
        wa_s TYPE ty_s.

DATA : s1 TYPE string,
        var TYPE i.


wa_s-s = 'KEY_KOSTL'APPEND wa_s TO it_s. CLEAR wa_s.
wa_s-s = 'KEY_KKKOSTL'. APPEND wa_s TO it_s. CLEAR wa_s.
wa_s-s = 'KEY__KOSTL'. APPEND wa_s TO it_s. CLEAR wa_s.
wa_s-s = 'KEY_KEOSTL'. APPEND wa_s TO it_s. CLEAR wa_s.


LOOP AT it_s INTO wa_s.
   var = STRLEN( wa_s-s ).
   var = var - 4.
   s1 = wa_s-s+var(4).
   WRITE : / s1.
   CLEAR : wa_s, var, s1.
ENDLOOP.

Output


Former Member
0 Kudos

Hi Sebastian,

For SHIFT statement in ABAP:

  1. The characters listed after the LEADING statement are treated as a pattern. These are not considered as a sequence of characters which should match, i.e. they are not a substring
  2. The content of the field is moved as long as the first or last character is contained in the pattern.

In your case, string is 'KEY_KOSTL' and what you want to shift is 'KEY_'. Now remaining string contains 'KOSTL', first letter of which is 'K' which is same as the first letter of a string which you want to delete. This is the reason it is giving output as 'OSTL'.

Now try using 'KEY_BOSTL' sting and delete leading 'KEY_'. And then if you execute your output will be 'BOSTL'.

Regards,

PS

Former Member
0 Kudos

Hi Sebastian,

s = "KEY_KOSTAL"

SHIFT s DELETING LEADING "KEY_".

In this  the shift statement would continue to delete the letters mentioned in the pattern.

"KEY_" is a pattern in your case, so after deleting first KEY_, it gets again first letter as K which it will delete till the pattern is over, which in this case ends at K.

If you want to achieve the objective, use string function "strlen".

data : lv_pattern type string VALUE 'KEY_',

          lv_string type string VALUE KEY_KOSTAL',

          l_length typei.

l_length = strlen (lv_pattern).

SHIFT lv_string by l_length PLACES LEFT.

Hope it helps..

Regards

Sumit Sharma

former_member184569
Active Contributor
0 Kudos

Hi Sumit,

DATA s TYPE string.

data s1 type string.

s = 'KEY_KOSTL'.

s1 = 'KO'

SHIFT S UP TO S1.

SHIFT S UP TO S1 LEFT.

Both statements should work.

Former Member
0 Kudos

Hello Sebastian,

     

      SHIFT statement looks for pattern not the sequence or ward as whole.

So following leading characters 'K' , 'E', 'Y','_' will be truncated.

Cheers

former_member184569
Active Contributor
0 Kudos

Another option that you could use is.

  1. REPLACE [ FIRST OCCURRENCE ] OF 'KEY_'  IN str WITH  '  '.

* First occurence is optional since this is also default setting.

  2.  CONDENSE str.

          or

     SHIFT STR LEFT DELETING LEADING SPACE.

Former Member
0 Kudos

Hi ,

Reason of your code not working fine is that the shift statement matches the sub string matches with the main string and remove the content from  string matching to the sub string.

Ex 

s = 'KEY_KEYOSTL'.
SHIFT s LEFT DELETING LEADING 'KEY_'. Out put will be OSTL.

So use this method in such cases.

DATA s TYPE string.
s = 'KEY_KOSTL'.
SHIFT s By 4 PLACES LEFT.

This works fine.

Regards.

SebastianK
Explorer
0 Kudos

Thank you for your contributions! I wasn't aware of that pattern thing, thought it could work with a whole string shifting. I will do the SHIFT with the addition 4 PLACES LEFT now.