Application Development and Automation 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: 
Read only

SHIFT UP TO RIGHT statement return whole words

former_member607883
Discoverer
0 Kudos
1,246

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.

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
0 Kudos
1,133

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`.
3 REPLIES 3
Read only

Sandra_Rossi
Active Contributor
0 Kudos
1,134

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`.
Read only

0 Kudos
1,133

OK I get it now. So it was data type that cause these.Thank you very much.

Read only

Sandra_Rossi
Active Contributor
0 Kudos
1,133

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'.