Processing Sections of Strings
You can address a section of a string in any statement in which non-numeric elementary ABAP types or structures that do not contain internal tables occur using the following syntax:
<f>[+<o>][(<l>)]
By specifying an offset +<o> and a length (<l>) directly after the field name <f>, you can address the part of the field starting at position <o>+1 with length <l> as though it were an independent data object. The data type and length of the string section are as follows:
Original field |
Section | |
Data type |
Data type |
Length |
C |
C |
<l> |
D |
N |
<l> |
N |
N |
<l> |
T |
N |
<l> |
X |
X |
<l> |
Structure |
C |
<l> |
If you do not specify the length <l>, you address the section of the field from <o> to the end of the field. If the offset and length combination that you specify leads to an invalid section of the field (for example, exceeding the length of the original field), a syntax or runtime error occurs. You cannot use offset and length to address a literal or a text symbol.
You should take particular care when addressing components of structures. To ensure the correct platform-specific alignment of type I and F components, they may contain filler fields, whose lengths you need to consider when calculating the correct offset. Furthermore, SAP plans to convert the internal representation of character types to UNICODE, in which one character will no longer occupy one byte, but instead two or four. Although offset and length specifications can work for character fields (types C, D, N, and T) or for hexadecimal fields (type X), incompatible changes may occur in structures containing a mixture of numeric, character, and hexadecimal fields. SAP therefore recommends that you do not use offset and length to address components of structures.
In nearly all cases, you must specify offset <o> and length <l> as numeric literals without a preceding sign. You may specify them dynamically in the following cases:
DATA TIME TYPE T VALUE '172545'.
WRITE TIME.
WRITE / TIME+2(2).
CLEAR TIME+2(4).
WRITE / TIME.
The output appears as follows:
172545
25
170000
First, the minutes are selected by specifying an offset in the WRITE statement. Then, the minutes and seconds are set to their initial values by specifying an offset in the clear statement.
Offset and Length Specifications in the MOVE Statement
For the MOVE statement, the syntax for specifying offset and length is as follows:
MOVE <f1>[+<o1>][(<l1>)] TO <f2>[+<o2>][(<l2>)].
Or, when you use the assignment operator:
<f2>[+<o2>][(<l2>)] = <f1>[+<o1>][(<l1>)].
The contents of the part of the field <f1> which begins at position <o1>+1 and has a length of <l1> are assigned to field <f2>, where they overwrite the section which begins at position <o2>+1 and has a length of <l2>.
In the MOVE statement, all offset and length specifications can be variables. This also applies to statements with the assignment operator, as long as these can also be written as MOVE statements. In statements where no field name is specified after the assignment operator, (for example, in Numeric Operations), all offset and length specifications must be unsigned number literals.
SAP recommends that you assign values with offset and length specifications only between non-numeric fields. With numeric fields, the results can be meaningless.
DATA: F1(8) VALUE 'ABCDEFGH',
F2(20) VALUE '12345678901234567890'.
F2+6(5) = F1+3(5).
In this example, the assignment operator functions as follows:
DATA: F1(8) VALUE 'ABCDEFGH',
F2(8).
DATA: O TYPE I VALUE 2,
L TYPE I VALUE 4.
MOVE F1 TO F2. WRITE F2.
MOVE F1+O(L) TO F2. WRITE / F2.
MOVE F1 TO F2+O(L). WRITE / F2.
CLEAR F2.
MOVE F1 TO F2+O(L). WRITE / F2.
MOVE F1+O(L) TO F2+O(L). WRITE / F2.
This produces the following output:
ABCDEFGH
CDEF
CDABCD
ABCD
CDEF
First, the contents of F1 are assigned to F2 without offset specifications. Then, the same happens for F1 with offset and length specification. The next three MOVE statements overwrite the contents of
Offset and Length Specifications in the WRITE TO Statement
For the WRITE TO statement, the syntax for specifying offset and length is as follows:
WRITE <f1>[+<o1>][(<l1>)] TO <f2>[+<o2>][(<l2>)].
The contents of the part of the field <f1> which begins at position <o1>+1 and has a length of <l1> are converted to a character field and assigned to field <f2>, where they overwrite the section which begins at position <o2>+1 and has a length of <l2>.
In the WRITE TO statement, the offset and length specifications of the target field can be variables. The offset and length of the target field must be numeric literals without a preceding sign.
DATA: STRING(20),
NUMBER(8) TYPE C VALUE '123456',
OFFSET TYPE I VALUE 8,
LENGTH TYPE I VALUE 12.
WRITE NUMBER(6) TO STRING+OFFSET(LENGTH) LEFT-JUSTIFIED.
WRITE: / STRING.
CLEAR STRING.
WRITE NUMBER(6) TO STRING+OFFSET(LENGTH) CENTERED.
WRITE: / STRING.
CLEAR STRING.
WRITE NUMBER TO STRING+OFFSET(LENGTH) RIGHT-JUSTIFIED.
WRITE: / STRING.
CLEAR STRING.
This produces the following output:
123456
123456
123456
The first six characters of the field NUMBER are written left-justified, centered, and right-justified into the last 12 characters of the field STRING.