REPLACE
[
FIRST OCCURRENCE OF | ALL OCCURRENCES OF ]
[
SUBSTRING ] old
IN [ SECTION OFFSET off LENGTH len OF ] text
WITH new.
1. ... FIRST
OCCURRENCE OF
2. ... ALL
OCCURRENCES OF
3.
... SUBSTRING
4. ... SECTION OFFSET off LENGTH len
5. ... IGNORING
CASE
6. ... RESPECTING
CASE
7. ... IN
BYTE MODE
8. ... IN
CHARACTER MODE
9.
... REPLACEMENT COUNT count
10.
... REPLACEMENT OFFSET roff
11. ... REPLACEMENT LENGTH rlen
The first four additions must be written in exactly the same position as shown in the basic form of the statement above, followed by the other additions.
Depending on whether byte or character string processing is carried out, the operands can only be byte-type or character-type (see Overview).
The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas.See You Can Only Use Character-Type Fields When Processing Strings.
In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs.For more details, see Processing Strings.
In the string text, replaces the first occurrence of the
search string
old with the content of new.
In the fields old and new,
trailing spaces at the end of character-type fields are ignored; in text conversely, they are taken into account.
System values
The Return Code indicates whether or not new has replaced old:
sy-subrc = 0
The string was found and replaced.
sy-subrc = 2
The string was found. However, significant characters at the end of text
were truncated. When old was replaced, the string became
longer than the defined length, so the characters after this defined length were ignored. All characters
other than trailing blanks are considered significant in character-type fields. In type X fields, these are all hex values.
sy-subrc = 4
The string text was not changed, because the system could not find the search string old.
sy-subrc = 8
This return value is only returned in systems with a multi-byte codepage. It indicates that either old or new contains an invalid multi-byte string.
DATA: text TYPE STRING.
text = 'abcdefgh'.
REPLACE 'd' IN text WITH 'XXXX'.
returns: text = 'abcXXXXefgh', sy-subrc = 0
... FIRST OCCURRENCE OF
In the string text, replaces the first occurrence of the
search text old with new. This addition is also the default setting.
DATA: myText type string.
myText = 'abcabcabcabcabc'.
REPLACE FIRST OCCURRENCE OF 'abc' IN myText WITH 'XYZ'.
returns: myText = 'XYZabcabcabcabc', sy-subrc = 0
... ALL OCCURRENCES OF
In the string text, replaces all patterns found that do
not overlap. Bear in mind that the system searches the entire string and then replaces old with new.
DATA: c4(4) TYPE C.
c4 = 'abab'.
REPLACE ALL OCCURRENCES OF 'ab' IN c4 WITH 'CCC'.
returns: c4 = 'CCCC', sy-subrc = 2
not c4 = 'CCCa', sy-subrc = 2. (as it would, if the system replaced each search string successively).
DATA: myText type string.
myText = 'abcabcabcabcabc'.
REPLACE ALL OCCURRENCES OF 'abc' IN myText WITH 'XYZ'.
returns: myText = 'XYZXYZXYZXYZXYZ', sy-subrc = 0
...SUBSTRING
This addition is also the default setting. It specifies that the search string is to be interpreted as a string. This distinction is made for use in future enhancements.
... SECTION OFFSET off LENGTH len
This addition stipulates that the system will only search in the part of the string text specified by
the offset
off and the length len.
If you use the SECTION... addition, you must also use
either OFFSET... or LENGTH...
or both. The default values for OFFSET = 0, while for
LENGTH it = -1. The length declaration -1
represents the remaining length of the string text, that is the length from the offset off to the end of the string. The area specified by the section declaration must lie within the string.
...IGNORING CASE
This addition specifies that the system ignores the case (upper or lower) of letters when searching.
DATA: text(50) TYPE C,
old TYPE
STRING,
new(5) TYPE C.
text =
'kEin eI GlEIcht dem Anderen'.
old = 'ei'.
new = 'XX'.
REPLACE ALL OCCURRENCES OF old IN text WITH new IGNORING CASE.
returns: text = 'kXXn XX GlXXcht dem Anderen', sy-subrc = 0
... RESPECTING CASE
This addition is the default setting. The system matches the case of the letters when searching.
DATA: text(20) TYPE C.
text = 'ABCDEF'.
REPLACE 'a' IN text WITH 'XXXX' respecting case.
liefert: text = 'ABCDEF', sy-subrc = 4
...IN BYTE MODE
This addition specifies that the string text, the search
string
old and the replacement string new must have the type X or XSTRING.
You cannot use this addition with the IGNORING CASE or RESPECTING CASE addition.
...IN CHARACTER MODE
This addition specifies that the string text, the search
string
old and the replacement string new must be
character-type - that is, have the
data type C, D,
N, T, or STRING.
This addition is optional and corresponds to the default setting.
...REPLACEMENT COUNT count
The number of search strings that have been successfully replaced is stored in the field count.
If the string text is of a C or X type, and part of the
replaced text exceeds the defined length of text, the system counts only successfully replaced strings.
DATA: text(10) TYPE C,
old TYPE
STRING,
new TYPE STRING,
rcount TYPE I.
text = 'aBaBaBaBaB'.
*
0123456789
old = 'a'.
new = 'CCC'.
REPLACE ALL OCCURRENCES OF old IN text WITH new REPLACEMENT COUNT rcount.
returns: CCCBCCCBCC, rcount = 3
Note that the replacement count remains unchanged if the replacement was unsuccessful. In this case,
only the system field sy-subrc tells you whether the replacement has taken place at all.
DATA: cnt TYPE i,
str TYPE string VALUE 'xyxy'.
REPLACE
ALL OCCURRENCES OF 'xy' IN str WITH 'CC' REPLACEMENT COUNT cnt.
REPLACE ALL OCCURRENCES OF 'x' IN str WITH 'b' REPLACEMENT COUNT cnt.
In both cases, the replacement count cnt = 2, although
no replacement has taken place in the second case. In the first case, sy-subrc
= 0, in the second case, sy-subrc is set to 4. This behaviour
corresponds to the ABAP default since parameters always remain unchanged if a desired action could not be performed.
...REPLACEMENT OFFSET roff
Writes the offset of the last completely or partially replaced search string text to the file roff.
DATA: text(20) TYPE C,
old TYPE
STRING,
new TYPE STRING,
roff TYPE I.
text = 'Keiner
weiß Bescheid'.
* 01234567890123456789
old =
'ei'.
new = 'XXXX'.
REPLACE ALL OCCURRENCES OF old IN text WITH new REPLACEMENT OFFSET roff.
returns: text = 'KXXXXner wXXXXß Besc', sy-subrc = 2, roff = 8
The value returned by the REPLACEMENT ... addition always
refers to the start of the string text, even if you also
use the SECTION... addition - that is, it is an absolute value.
DATA: text TYPE STRING,
new(5) TYPE C,
roff
TYPE I.
text = '0123456789abcdefghijkl'.
new = 'XXXX'.
REPLACE 'abc' IN SECTION OFFSET 5 LENGTH -1 OF text WITH new
REPLACEMENT OFFSET roff.
returns: text = '0123456789XXXXdefghijkl', sy-subrc = 0, roff = 10
...REPLACEMENT LENGTH rlen
Writes the length of the last used replacement string to the field rlen.
DATA: input TYPE STRING,
old(10) TYPE C,
new(10)
TYPE C,
rlen TYPE I.
input =
'myLoginName'.
old = 'LoginName'.
new = 'XX'.
REPLACE old IN input WITH new REPLACEMENT LENGTH rlen.
returns: input = 'myXX', sy-subrc = 0, rlen = 2
Catchable Exceptions
CX_SY_REPLACE_INFINITE_LOOP
CX_SY_RANGE_OUT_OF_BOUNDS
Replacing the Contents of Fields.