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 pattern IN 

REPLACE - options

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... [{RESPECTING|IGNORING} CASE] 
    [REPLACEMENT COUNT rcnt]
    { {[REPLACEMENT OFFSET roff]
       [REPLACEMENT LENGTH rlen]}
    | [RESULTS result_tab|result_wa] } ...

Extras

1. ... {RESPECTING|IGNORING} CASE

2. ... REPLACEMENT COUNT rcnt

3. ... REPLACEMENT OFFSET roff

4. ... REPLACEMENT LENGTH rlen

5. ... RESULTS result_tab|result_wa

Effect

These additions modify the statement REPLACE pattern IN and provide advanced evaluation options. The addition CASE can be used to specify whether the search is case-sensitive. The additions REPLACEMENT and RESULTS can be used to determine the number, position, and length of the string(s) replaced.

Addition 1

... {RESPECTING|IGNORING} CASE

Effect

You can use this addition in string processing only. It has the same syntax and effect as the associated addition for searching for a substring in a data object using the FIND statement. This addition is not permitted when using an instance of class CL_ABAP_REGEX.


Example

Replacement of all uppercase characters in a string by a backquote. If RESPECTING CASE is not specified, all characters are replaced.

DATA(str) = `Hello`. 
REPLACE ALL OCCURRENCES OF REGEX `\u` IN str WITH '`' 
        RESPECTING CASE. 
cl_demo_output=>display( str ). 

Addition 2

... REPLACEMENT COUNT rcnt

Effect

This addition saves the number of replacements made in data object dobj to rcnt. The following can be specified for rcnt:

  • An existing variable that expects the data type i.
  • An inline declaration DATA(var). The declared variable has the data type i.

If no replacements are made, rcnt is set to 0.


Note

In data objects with a fixed length, the number of replacements made in data object dobj can be less than the number of occurrences.

Addition 3

... REPLACEMENT OFFSET roff

Effect

This addition saves the offset with respect to data object dobj at which the last replacement was made to roff. The following can be specified for roff:

  • An existing variable that expects the data type i.
  • An inline declaration DATA(var). The declared variable has the data type i.

If no replacement is made, roff retains its existing value or stays initial.


Notes

  • When ALL OCCURRENCES is used, REPLACEMENT OFFSET generally returns a different value than MATCH OFFSET for the FIND statement. This is because the position of the last location found can be shifted by previous replacements.
  • In data objects of fixed length, the value in roff refers to the last replacement within the data object. Occurrences that are shifted by previous replacements in the data object are no longer relevant.

Addition 4

... REPLACEMENT LENGTH rlen

Effect

This addition saves the length of the last substring inserted into dobj to rlen. The following can be specified for rlen:

  • An existing variable that expects the data type i.
  • An inline declaration DATA(var). The declared variable has the data type i.

If no replacement is made, rlen retains its existing value or stays initial.


Note

In data objects with a fixed length, the length of the last string inserted can be shorter than the length of new if the intermediate result is truncated.


Example

Pattern-based replacement of all occurrences of the substring "know" in the data objects 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. The data objects cnt, off, and len have the values 2, 23, and 16. After the second REPLACE statement, text2 contains the truncated content "I should know that" and sy-subrc contains 2. The data objects cnt, off, and len have the values 1, 2, and 16.

DATA: text1 TYPE string, 
      text2 TYPE c LENGTH 18. 

text1 = text2 = 'I know you know'. 

REPLACE ALL OCCURRENCES OF 'know' IN 
        text1 WITH 'should know that' 
              REPLACEMENT COUNT DATA(cnt) 
              REPLACEMENT OFFSET DATA(off) 
              REPLACEMENT LENGTH DATA(len). 

REPLACE ALL OCCURRENCES OF 'know' IN 
        text2 WITH 'should know that' 
              REPLACEMENT COUNT  cnt 
              REPLACEMENT OFFSET off 
              REPLACEMENT LENGTH len. 

Addition 5

... RESULTS result_tab|result_wa

Effect

If at least one replacement is made, the RESULTS addition saves the offsets of the locations at which replacements were made. It also saves the length of the substrings inserted either in an internal table result_tab or in a structure result_wa. The syntax and meaning of the addition are otherwise the same as those for the addition of the same name for the FIND statement. The only difference is that in this case, the data types for result_tab and result_wa must be REPL_RESULT_TAB and REPL_RESULT, in which there is no SUBMATCHES component. As in FIND, an inline declaration DATA(var) can be specified after RESULTS.


Example

A result table results is declared inline and supplied with the properties of the seven replaced characters.

DATA(str) = `Diddeldummdideldei`. 
REPLACE ALL OCCURRENCES OF `d` IN str WITH 't' 
        IGNORING CASE 
        RESULTS DATA(results). 
cl_demo_output=>display( results ).