SAP REPLACE FUNCTIONS



Get Example source ABAP code based on a different SAP table
  


• replace ABAP_FUNCTION
• with replace function

ABAP_STRINGFUNC - replace

ABAP_SYNTAX
1. ... replace( val = text $[ off = off$] $[ len = len$]
with = new ) ...
2. ... replace( val = text ${ sub = substring $}$|${pcre = regex$}
with = new $[case = case $] $[occ = occ$] ) ...

What does it do?
This built-in function replaces a substring of text with the character string specified in new and returns the changed text.
The substring is determined as follows:
The variant with the arguments off and len replaces the substring determined by the offset off and the length len. At least one of these additions must be defined.
The variant with the arguments sub or pcre searches the text for the occurrence specified in occ for a match with the substring specified in substring or with a regular expression specified in regex and replaces the occurrence. 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 occ contains the value 0, all occurrences are replaced. 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 unchanged content of text is returned.
new is a character-like expression position. If this has a fixed length, trailing blanks are ignored.
The return value has the type string.



Latest notes:

Borderline cases for the variants with the arguments off and len:
If only off is specified or if the value 0 is specified for len, replace works like the insert function.
If only len is specified or if off has the value 0, the first segment of the length len is replaced.
If the value of off is equal to the length of text, the value of len must be equal to 0 or len is not specified. The character string new is then appended to the end of text .
If a regular expression is used with regex, special replacement patterns that allow references to the respective occurrences can be specified in new. It should be noted that in regular replacement texts, the associated special characters $, , `, and ยด as well as the escape character must be transformed to literal characters using the prefix .
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 result of the following replacement is aa_XX_cc
The substring bb starting at offset 3 having a length of 2 is replaced with XX.
ABEXA 01579
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
The result of the following replacements are aa_bb_cc
and aabbcc
First, all occurrences (occ = 0) of substring - are replaced with _, then these are replaced by an empty string, hence removed.
ABEXA 01577
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
The result of the following replacement is < title>Th < b > i < /b>s < b > i < /b>s the < i > T < b > i < /b>tle < /i> < /title>.
In an HTML line, a particular letter is surrounded by format tags if it is not within a tag itself.
ABEXA 01230
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.
Runtime error:
STRG_ILLEGAL_PAR