09-27-2013 1:49 PM
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
09-27-2013 9:08 PM
09-27-2013 9:20 PM
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.
09-28-2013 2:39 AM
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.
09-28-2013 4:53 AM
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
09-28-2013 11:45 AM
Hi Sebastian,
For SHIFT statement in ABAP:
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
09-28-2013 2:00 PM
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
09-29-2013 12:53 PM
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.
09-29-2013 1:44 PM
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
09-29-2013 6:45 PM
Another option that you could use is.
* First occurence is optional since this is also default setting.
2. CONDENSE str.
or
SHIFT STR LEFT DELETING LEADING SPACE.
09-30-2013 5:16 AM
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.
09-30-2013 7:46 AM
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.