Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  ABAP Syntax →  ABAP Statements →  Operands →  Data Objects in Operand Positions 

Substring Access

In operand positions, subareas of certain data objects can be accessed by specifying an offset/length:

Other versions: 7.31 | 7.40 | 7.54

Syntax


dobj[+off][(len)]

<fs>[+off][(len)]

dref->*[+off][(len)]

Effect

A specified offset or length is directly appended to either the dobj descriptor of the data object, an <fs> field symbol, or a dereferenced data reference variable dref->*: Offsets/lengths can be specified for:

  • Flat structures where one of the following prerequisites must be fulfilled:
  • The structure contains flat character-like components only.
  • The first Unicode fragment of the structure is flat and character-like, and the substring addressed by specifying the offset and length is located within this fragment.

The following restrictions apply:

  • In write positions, only flat data objects are permitted; it is not possible to write to substrings of strings.
  • Access to substrings of strings is also not possible in the following reader positions:
  • dobj[+off][(len)] specified as a memory area mem_area of the statement ASSIGN.
  • dobj[+off][(len)] specified as an argument of the statement GET REFERENCE or the reference operator REF.
  • Offsets/lengths cannot be specified for literals or text symbols.
  • In the case of dynamically specified operands in parentheses, no lengths can be specified.
  • A dereferenced data reference variable dref->* must be typed in full.
  • For a writable expression, offsets/lengths cannot be specified as memory areas in the statement ASSIGN or as arguments of the reference operator REF (table expressions only).

The segment of the data object is used that has the offset specified in off and the length (in characters or bytes) specified in len. A memory area must not be addressed outside the field boundaries, except in the case of the ASSIGN statement. For an offset specified without a length, the entire substring is addressed as of the off character; for a length specified without an offset, the first len characters are addressed (different rules apply to the ASSIGN statement).

The operands off and len expect data objects of the type i. These data objects must contain positive integers, with the following exceptions.

  • The length 0 can be specified for strings.
  • A negative offset (but never length 0) can be specified if an <fs> field symbol is specified in the ASSIGN statement for dobj.
  • If off is less than the length of dobj, an asterisk (*) can be specified for len. The upper limit of dobj then determines the upper limit of the memory area.

If the prerequisites are not met or if the subarea defined by off and len is not completely contained in the data object (except in the case of ASSIGN), a syntax error occurs (if statically identifiable). Otherwise, an exception of class CX_SY_RANGE_OUT_OF_BOUNDS is raised. If off is specified as a numeric literal, then this literal cannot be prefixed with a sign.

The offset and length specified are counted in characters for character-like data objects and in bytes for all other data objects. A character is equivalent to a byte in non- Unicode systems.

A substring specified by an offset or length is handled like a data object of the specified length for which the data type depends on the data type of the original data object, the field symbol, or the data reference variable, as shown below:

Original Data Type Data Type of Substring
c c
d n
n n
t n
string string
x x
xstring xstring
Structure type c

If the length of the substring exactly corresponds to the length of the structure in a substring access to a structure, the substring does not have data type c; instead, it is handled like the structure itself.


Notes

  • For reads on substrings of character-like data objects, predefined substring functions that allow searches by specifying both substring and offset/lengths are available.

  • It is a good idea to specify offsets with the value 0 explicitly too (that is, dobj+0(len) instead of dobj(len)). This distinguishes a substring access in the source code clearly from other language constructs that also use parentheses, such as dynamic specifications, method calls, or inline declarations.

  • No substring access cnt(len), sum(len) can be performed on data objects called cnt and sum unless an offset is specified explicitly. The compiler always interprets this as a number or sum of a field len in the control level processing of an extract dataset.

  • The statement MOVE PERCENTAGE indicates an obsolete form of substring access.

  • dobj+0(*), dobj+0, or dobj(*) are always interpreted as dobj. In this case, dobj can also be a data object where substring access according to the rules above is not possible.

Example

The following structure has both character-like and non-character-like components:

DATA: 
  BEGIN OF struc, 
    a TYPE c LENGTH 3,    "Length 3 characters 
    b TYPE n LENGTH 4,    "Length 4 characters 
   c TYPE d,             "Length 8 characters 
    d TYPE t,             "Length 6 characters 
    e TYPE decfloat16,    "Length 8 bytes 
    f TYPE c LENGTH 28,   "Length 28 characters 
    g TYPE x LENGTH 2,    "Length 2 bytes 
  END OF struc.

The Unicode fragment view splits the structure into five areas, F1 - F5.

[ aaa | bbbb | cccccccc | ddd | AAA | eeee | fffffffffffff | gg ]
[            F1               |  F2 |  F3  |       F4      | F5 ]

In Unicode systems, offset/length accesses are possible on the character-like initial fragment F1 only, for example struc(21) or struc+7(14). An access such as struc+57(2), for example, is not permitted in Unicode systems.

Continue

Substrings - Example