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
substring, substring_... - Substring Functions
Other versions: 7.31 | 7.40 | 7.54
Syntax Forms
-
... substring( val = text [ off = off] [len = len] ) ...
-
... substring_from( val = text { sub = substring}|{ regex = regex}
[ case = case] [ occ = occ] [ len = len] ) ... -
... substring_after( val = text { sub = substring}|{ regex = regex}
[case = case] [ occ = occ] [ len = len] ) ... -
... substring_before( val = text { sub = substring}|{ regex = regex}
[case = case] [ occ = occ] [ len = len] ) ... -
... substring_to( val = text { sub = substring}|{ regex = regex}
[ case = case] [ occ = occ] [ len = len] ) ...
Effect
In the argument text
, the substring functions determine a substring and return it.
The substring is determined as follows:
- The function
substring
uses the offsetoff
and the length len to return a certain subrange. At least one of the two argumentsoff
orlen
must be specified.
- The function
substring_from
scanstext
for the match (specified inocc
) with the character string specified insubstring
or with the regular expression specified inregex
and returns the subrange of the lengthlen
from the offset of the occurrence. Iflen
is not specified, the substring is returned to the end of the character string. Ifsubstring
is empty, an exception from the class CX_SY_STRG_PAR_VAL is raised. The search is case-sensitive by default, but this can be overridden using the parametercase
. If no substring is found, the return code is empty.
- The function
substring_after
works in the same way assubstring_from
, but the subrange plus the length of the substring found is returned, from the offset of the occurrence.
- The function
substring_before
works in the same way assubstring_from
, but the subrange of the lengthlen
is returned, before the offset of the occurrence. Iflen
is not specified, the subrange is formed from the start of the character string.
- The function
substring_to
works in the same way assubstring_before
, but the subrange before the offset of the occurrence plus the length of the substring found is returned.
The return code has the type string
accordingly.
Note
The performance of the substring functions is not as good as a direct substring access. They do, however, make it possible to use expressions in all operand positions.
Example
The return codes of the following function calls are: "CD", "CDEFGH", "EFGH", "AB", and "ABCD".
DATA result TYPE string.
...
result = substring( val = 'ABCDEFGH' off = 2 len = 2 ).
...
result = substring_from( val = 'ABCDEFGH' sub = 'CD' ).
...
result = substring_after( val = 'ABCDEFGH' sub = 'CD' ).
...
result = substring_before( val = 'ABCDEFGH' sub = 'CD' ).
...
result = substring_to( val = 'ABCDEFGH' sub = 'CD' ).
...
Exceptions
Handleable Exceptions
CX_SY_RANGE_OUT_OF_BOUNDS
-
Cause: Illegal offset or length specified in
off
andlen
.
Runtime error:STRING_OFFSET_TOO_LARGE
CX_SY_REGEX_TOO_COMPLEX
-
Cause: More information: Exceptions in Regular Expressions.
Runtime error:REGEX_TOO_COMPLEX
CX_SY_STRG_PAR_VAL
-
Cause: Substring in
sub
or regular expression inregex
is empty or occurrence inocc
is 0.
Runtime error:STRG_ILLEGAL_PAR