2022 May 05 7:42 AM
If I understand correctly (By read the SAP Documentation and many example code from the website). The SHIFT <string> UP TO <needle> RIGHT statement will find needle in string. If it find out it will return the substring start from the first letter of the needle to the begining of the word. So I coding like this:
DATA text TYPE string.
text = `12345`.
SHIFT text UP TO `3`.
write / `UP TO = ` && text.
text = `12345`.
SHIFT text UP TO `3` CIRCULAR.
write / `UP TO CIRCULAR = ` && text.
text = '12345'.
SHIFT text UP TO '3' RIGHT.
write / `UP TO RIGHT = ` && text.
text = `12345`.
SHIFT text UP TO `3` RIGHT CIRCULAR.
write / `UP TO RIGHT CIRCULAR = ` && text.
Why did it output like this? Not 123.
2022 May 05 10:02 AM
It's not clear with a screenshot what result you get. Actual results are as follows:
DATA text TYPE string.
text = `12345`.
SHIFT text UP TO `3`. " NB: LEFT by default
ASSERT text = `345`.
text = `12345`.
SHIFT text UP TO `3` CIRCULAR.
ASSERT text = `34512`.
text = '12345'.
SHIFT text UP TO '3' RIGHT.
ASSERT text = ` 12345`. " <============= why?
text = `12345`.
SHIFT text UP TO `3` RIGHT CIRCULAR.
ASSERT text = `45123`.
As ABAP documentation says, "Data objects of type STRING or xstring are truncated by the number of shifted places when shifted to the left and lengthened by the number of shifted places when shifted to the right."
In your case, the initial string is 5 characters long, you do a right shift so it aligns "3" so that to be the 5th character, so it shifts the whole string by 2 places to the right, left is completed by spaces, and the final length is 7 characters.
To make it more clear, UP TO '2' is a better example:
text = '12345'.
SHIFT text UP TO '2' RIGHT.
ASSERT text = ` 12345`.
2022 May 05 10:02 AM
It's not clear with a screenshot what result you get. Actual results are as follows:
DATA text TYPE string.
text = `12345`.
SHIFT text UP TO `3`. " NB: LEFT by default
ASSERT text = `345`.
text = `12345`.
SHIFT text UP TO `3` CIRCULAR.
ASSERT text = `34512`.
text = '12345'.
SHIFT text UP TO '3' RIGHT.
ASSERT text = ` 12345`. " <============= why?
text = `12345`.
SHIFT text UP TO `3` RIGHT CIRCULAR.
ASSERT text = `45123`.
As ABAP documentation says, "Data objects of type STRING or xstring are truncated by the number of shifted places when shifted to the left and lengthened by the number of shifted places when shifted to the right."
In your case, the initial string is 5 characters long, you do a right shift so it aligns "3" so that to be the 5th character, so it shifts the whole string by 2 places to the right, left is completed by spaces, and the final length is 7 characters.
To make it more clear, UP TO '2' is a better example:
text = '12345'.
SHIFT text UP TO '2' RIGHT.
ASSERT text = ` 12345`.
2022 Jun 07 9:20 AM
OK I get it now. So it was data type that cause these.Thank you very much.
2022 May 05 10:45 AM
This works as intended with type C instead of STRING:
DATA text TYPE c LENGTH 5.
text = '12345'.
SHIFT text UP TO '3' RIGHT.
ASSERT text = ' 123'.