Show TOC

Moving Field Contents Character by CharacterLocate this document in the navigation structure

Moving Field Contents Character by Character

You can shift the contents of a field using the following variants of the SHIFT statement. SHIFT moves field contents character by character.

Shifting a Character String by a Given Number of Positions

SHIFT c [BY n PLACES] [mode].

This statement shifts the field c by npositions. If you omit BY n PLACES, n is interpreted as one. If n is 0 or negative, c remains unchanged. If n exceeds the length of c, c is filled out with blanks. n can be variable.

With the different mode options, you can shift the field c in the following ways:

mode is LEFT:

Shifts the field contents n places to the left and adds n blanks at the right-hand end of the field (default).

mode is RIGHT:

Shift n positions to the right and adds n blanks at the left-hand end of the field.

mode is CIRCULAR:

Shifts the field content on a cyclical basis: shifts content npositions to the left and shifts the n characters on the left to the right.

Tip

DATA: t(10) TYPE c VALUE 'abcdefghij',      string LIKE t.

string = t.WRITE string.SHIFT string.WRITE / string.string = t.SHIFT string BY 3 PLACES LEFT.WRITE / string.string = t.SHIFT string BY 3 PLACES RIGHT.WRITE / string.string = t.SHIFT string BY 3 PLACES CIRCULAR.WRITE / string.

Output:

abcdefghij

bcdefghij

defghij

abcdefg

defghijabc

Shifting the Field Content up to a Given Character String

SHIFT c UP TO str mode.

This statement searches the field contents of c until it finds the string str and shifts the field c up to the edge of the field. The options mode are the same as above. str can be a variable.

If str is not found in c, sy-subrc is set to 4 and c is not shifted. Otherwise, sy-subrc is set to 0.

Tip

DATA: t(10) TYPE c VALUE 'abcdefghij',      string LIKE t,      str(2) TYPE c VALUE 'ef'.

string = t.WRITE string.

SHIFT string UP TO str.WRITE / string.

STRING = t.SHIFT string UP TO str LEFT.WRITE / string.

STRING = t.SHIFT string  UP TO str RIGHT.WRITE / string.

string = t.SHIFT string  UP TO str CIRCULAR.WRITE / string.

Output:

abcdefghij

efghij

efghij

 abcdef

efghijabcd

Shifting a Character String According to the First or Last Character

SHIFT c LEFT  DELETING LEADING  str.

SHIFT c RIGHT DELETING TRAILING str.

This statement shifts the field c to the left or to the right, provided the first character on the left or the last character on the right occur in str. The right or left of the field is then padded with blanks. str can be a variable.

Tip

DATA: t(14) TYPE c VALUE '    abcdefghij',      string LIKE t,      str(6) VALUE 'ghijkl'.

string = t.WRITE string.

SHIFT string LEFT DELETING LEADING SPACE.WRITE / string.

string = t.SHIFT string RIGHT DELETING TRAILING str.WRITE / string.

Output:

 abcdefghij

abcdefghij

 abcdef