ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Character String and Byte String Processing → Expressions and Functions for String Processing → String Functions → Processing Functions for Character-Like Arguments
segment - Segment Function
Other versions: 7.31 | 7.40 | 7.54
Syntax
... segment( val = text index = idx [sep|space = sep] ) ...
Effect
This function returns the occurrence of a segment of the argument text
specified
by index
. A segment
is defined by limiters. The beginning and ending of the string in text
are
the outer limiters. Inner limiters can be passed to sep
or space
as follows:
- If the argument
sep
is provided, then the substring specified in sep is searched for (case-sensitive as intext
) and used as a limiter. If a substring specified insep
occurs directly one after another intext
, an empty segment is created and an empty string is returned for this segment.
- If the argument
space
is provided, each individual character is searched for insep
(case-sensitive) and this is used as a limiter. If the individual characters specified insep
occur directly one after another intext
, no empty segment is created and no result is returned.
The limiters are not part of the segments. If sep
or space
are not specified, the argument sep
is implicitly filled with individual empty spaces. In both cases, specifying an empty string raises an exception of the class CX_SY_STRG_PAR_VAL.
If index
is positive, the occurrences are counted from the left; if
index is negative, the instance are counted from the right. The values 1, 2, .... indicate the
first, second, ... occurrences. The values -1, -2, .... indicate the last, last but one, ... occurrences.
If the value of index
is 0, or the specified segment does not exist, an exception of the class CX_SY_STRG_PAR_VAL is raised.
sep
is a character-like expression position and index
is a
numerical expression
position with type i
. If sep
has a fixed length, trailing blanks are ignored.
The return code has the type string
.
Note
If the substring specified in sep
is not found, the entire character string
forms a single segment. This segment can only be addressed by using the values 1 or -1 for index
.
Example
The following function calls produce "AB", "CD", "EF", "GH", respectively, and raise an exception at the end.
DATA result TYPE string.
DO.
TRY.
result = segment( val = 'AB\brCD\brEF\brGH'
index = sy-index
sep = `\br` ).
...
CATCH cx_sy_strg_par_val.
EXIT.
ENDTRY.
ENDDO.
DO.
TRY.
result = segment( val = 'AB CD - EF_GH'
index = sy-index
space = ` -_` ).
...
CATCH cx_sy_strg_par_val.
EXIT.
ENDTRY.
ENDDO.
Example
Also refer to the example String Functions, cmax
, cmin
and segment
.
Exceptions
Catchable Exceptions
CX_SY_STRG_PAR_VAL
-
Cause: Limiter in
sep
is empty or the occurrence inindex
is 0 or was not found.
Runtime Error:STRG_ILLEGAL_PAR