SAP SUBSTRING FUNCTIONS



Get Example source ABAP code based on a different SAP table
  


• substring ABAP_FUNCTION
• substring_after ABAP_FUNCTION
• substring_before ABAP_FUNCTION
• substring_to ABAP_FUNCTION
• substring_from ABAP_FUNCTION

ABAP_STRINGFUNC - substring, substring_...

ABAP_SYNTAX
1. ... substring( val = text $[ off = off$] $[ len = len$] ) ...
2. ... substring_from( val = text ${sub = substring $}$|${pcre = regex$}
$[case = case $] $[occ = occ$] $[ len = len$] ) ...
3. ... substring_after( val = text ${sub = substring $}$|${pcre = regex$}
$[case = case $] $[occ = occ$] $[ len = len$] ) ...
4. ... substring_before( val = text ${sub = substring $}$|${pcre = regex$}
$[case = case $] $[occ = occ$] $[ len = len$] ) ...
5. ... substring_to( val = text ${ sub = substring $}$|${pcre = regex$}
$[case = case$] $[ occ = occ$] $[ len = len$] ) ...

What does it do?
The built-in substring functions determine a substring in the argument text and return it.
The substring is determined as follows:
The function substring uses the offset off and the length len to return a certain substring. At least one of the arguments off or len must be specified.
The function substring_from searches text for the match specified in occ with the character string specified in substring or with the regular expression specified in regex and returns the subarea of the length len from the offset of the occurrence. If the argument pcre is used, regex must contain a PCRE regular expression. A regular expression in PCRE syntax is compiled in an extended mode: Most unescaped whitespace (blanks and line breaks) of the pattern are ignored outside character classes and comments can be placed behind #. In order to include whitespace and # into a pattern, they must be escaped or the extended mode must be switched of with (?-x) in the regular expression. If len is not specified, the substring is returned to the end of the character string. If substring is empty, an exception of the class CX_SY_STRG_PAR_VAL is raised. The search is case-sensitive by default, but this can be overridden using the parameter case. If no substring is found, the return value is empty.
The function substring_after works in the same way as substring_from, but the subarea plus the length of the substring found is returned, from the offset of the occurrence.
The function substring_before works in the same way as substring_from, but the subarea of the length len is returned before the offset of the occurrence. If len is not specified, the subarea is formed from the start of the character string.
The function substring_to works in the same way as substring_before, but the subarea before the offset of the occurrence plus the length of the substring found is returned.
The return value has the type string accordingly.



Latest notes:

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 and they allow searches for substrings that match regular expressions.
Besides pcre, also the obsolete argument regex can be used. Then an obsolete POSIX regular expression must be passed.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The return values of the following function calls are: CD, CDEFGH, EFGH, AB, and ABCD.
ABEXA 01321
ABAP_EXAMPLE_END



Runtime Exceptions



Catchable Exceptions
CX_SY_RANGE_OUT_OF_BOUNDS
Reason for error:
Illegal offset or length specified in off and len.
Runtime error:
STRING_OFFSET_TOO_LARGE
CX_SY_REGEX_TOO_COMPLEX
Reason for error:
More information: Exceptions in Regular Expressions.
Runtime error:
REGEX_TOO_COMPLEX
CX_SY_STRG_PAR_VAL
Reason for error:
Substring in sub or regular expression in regex is empty or occurrence in occ is 0.
Runtime error:
STRG_ILLEGAL_PAR