REPLACE SECTION [OFFSET off] [LENGTH len] OF text WITH new.
1. ...
IN BYTE MODE
2. ... IN CHARACTER MODE
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 in String Processing.
In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs.The second variant should no longer be used in Unicode programs. For more information, see String Processing in this guide.
Replaces the section of the field text specified by the
offset off and the length len, with the contents of the field new.
System values
The Return Code indicates whether or not new has replaced the relevant string in the field text:
sy-subrc = 0
The specified section of the field text was found and replaced.
sy-subrc = 2
The string created when the section was replaced was longer than the defined (original)
length of text. For this reason, significant characters
at the end of the field new were truncated. All characters
other than trailing blanks are considered significant in character-type fields. In type X fields, these are all hex values.
sy-subrc = 8
This return value is only returned in systems with a multi-byte codepage. It indicates that new contains an invalid multi-byte string.
DATA: text TYPE STRING.
text = 'abcdefgh'.
REPLACE SECTION OFFSET 4 OF text WITH 'XXXX'.
returns text = 'abcdXXXX' and sy-subrc = 0.
The OFFSET off LENGTH len addition specifies the substring
in the field text that is to be replaced. At least one
of these two additions (
OFFSET or LENGTH)
is mandatory. The default value for OFFSET = 0; the default
value for LENGTH = -1. , The length -1
represents the remaining length symbolically. The area specified by these two additions must lie within the field text.
... IN BYTE MODE
All fields must have the type X or XSTRING if you use this addition.
DATA: x11(11) TYPE X,
x5(5) TYPE X.
x11
= '496C6F7665426972676974'.
x5 = '2E4D6F6C6C'.
REPLACE SECTION OFFSET 6 OF x11 WITH x5 IN BYTE MODE.
returns x11 = 496C6F7665422E4D6F6C6C and sy-subrc = 0.
... IN CHARACTER MODE
This addition is optional and corresponds to the standard setting.
DATA: pattern(6) TYPE C VALUE 'ABC',
len TYPE
I,
repl_string(6) TYPE C VALUE '123456',
field(12) TYPE
C VALUE 'abcdeABCDE'.
REPLACE pattern WITH repl_string INTO field.
does not change
field, since 'ABC ' does not occur in 'abcdeABCDE '.
len = STRLEN( pattern ).
REPLACE pattern LENGTH len
WITH
repl_string
INTO field.
changes field to 'abcde123456D'.
Contents in Fields