Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Statements for Character String and Byte String Processing →  REPLACE 

REPLACE SECTION OF

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new 
                 [IN {CHARACTER|BYTE} MODE].

Addition


... IN {CHARACTER|BYTE} MODE

Effect

When position-based replacements are performed, the section in dobj is replaced starting at the offset specified in off for the length specified in len with the content of the operand new. off and len are numerical expression positions with the operand type i. new is a functional operand position.

At least one of the additions OFFSET or LENGTH must be specified. If an offset is specified but no length, the content of the data object dobj is replaced starting at the offset off and continuing to the end. If a length is specified but no offset, the offset 0 is used implicitly. The values of off and len must be greater than or equal to 0 and the section specified by off and len must libe located within the length of dobj.

Addition

... IN {CHARACTER|BYTE} MODE

Effect

The optional IN {CHARACTER|BYTE} MODE addition determines whether character string or byte string processing is carried out. If the addition is not specified, character string processing is carried out. Depending on the processing type, dobj and new have to be character-like or byte-like.


Example

The statement FIND is used to determine the offset and length of the first word "know" in the data objects text1 and text2 and to replace this section in a position-based manner in text1 and text2 with "should know that". After the first REPLACE statement, text1 contains the complete content "I should know that you know" and sy-subrc contains 0. After the second REPLACE statement, text2 contains the truncated content "I should know that" and sy-subrc contains 2.

DATA: text1 TYPE string, 
      text2 TYPE c LENGTH 18, 
      off  TYPE i, 
      len TYPE i. 

text1 = text2 = 'I know you know'. 

FIND 'know' IN text1 MATCH OFFSET off 
                     MATCH LENGTH len. 

REPLACE SECTION OFFSET off LENGTH len OF: 
                text1 WITH 'should know that', 
                text2 WITH 'should know that'. 

Exceptions


Catchable Exceptions

CX_SY_RANGE_OUT_OF_BOUNDS

  • Cause: Specified offset or length violated the bounds of the string dobj.
    Runtime Error: REFI_WRONG_SECTION