REPLACE SECTION OF ... WITH ...

Basic form

REPLACE SECTION [OFFSET off] [LENGTH len] OF text WITH new.

Extras:


1. ... IN BYTE MODE



2. ... IN CHARACTER MODE


Notes

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.

Effect

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.

Example

DATA: text TYPE STRING.
text = 'abcdefgh'.
REPLACE SECTION OFFSET 4 OF text WITH 'XXXX'.

returns text = 'abcdXXXX' and sy-subrc = 0.

Notes

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.

Exceptions

  1. The exception CX_SY_RANGE_OUT_OF_BOUNDS which can be handled is triggered if the offset or length declaration exceeds the boundaries specified by SECTION text.

  2. The exception CX_SY_REPLACE_INFINITE_LOOP which can be handled is triggered if the length declaration in SECTION text is 0.


Addition 1

... IN BYTE MODE

Effect

All fields must have the type X or XSTRING if you use this addition.

Example

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.



Addition 2

... IN CHARACTER MODE

Effect

This addition is optional and corresponds to the standard setting.

Example

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'.



Related

FIND, TRANSLATE, OVERLAY

Additional help

Contents in Fields