REPLACE ... IN ... WITH ...

Basic form

REPLACE
  [ FIRST OCCURRENCE OF | ALL OCCURRENCES OF ]
  [ SUBSTRING ] old
  IN [ SECTION OFFSET off LENGTH len OF ] text
  WITH new.

Extras:


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


Notes

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.

Effect

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.

Example

DATA: text TYPE STRING.
text = 'abcdefgh'.
REPLACE 'd' IN text WITH 'XXXX'.

returns: text = 'abcXXXXefgh', sy-subrc = 0

Addition 1

... FIRST OCCURRENCE OF

Effect

In the string text, replaces the first occurrence of the search text old with new. This addition is also the default setting.

Example

DATA: myText type string.
myText = 'abcabcabcabcabc'.
REPLACE FIRST OCCURRENCE OF 'abc' IN myText WITH 'XYZ'.

returns: myText = 'XYZabcabcabcabc', sy-subrc = 0



Addition 2

... ALL OCCURRENCES OF

Effect

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.

Example 1

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

Example 2

DATA: myText type string.
myText = 'abcabcabcabcabc'.
REPLACE ALL OCCURRENCES OF 'abc' IN myText WITH 'XYZ'.

returns: myText = 'XYZXYZXYZXYZXYZ', sy-subrc = 0

Addition 3

...SUBSTRING

Effect

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.

Addition 4

... SECTION OFFSET off LENGTH len

Effect

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.

Addition 5

...IGNORING CASE

Effect

This addition specifies that the system ignores the case (upper or lower) of letters when searching.

Example

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



Addition 6

... RESPECTING CASE

Effect

This addition is the default setting. The system matches the case of the letters when searching.

Example

DATA: text(20) TYPE C.
text = 'ABCDEF'.
REPLACE 'a' IN text WITH 'XXXX' respecting case.

liefert: text = 'ABCDEF', sy-subrc = 4



Addition 7

...IN BYTE MODE

Effect

This addition specifies that the string text, the search string old and the replacement string new must have the type X or XSTRING.

Note

You cannot use this addition with the IGNORING CASE or RESPECTING CASE addition.



Addition 8

...IN CHARACTER MODE

Effect

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.

Note

This addition is optional and corresponds to the default setting.



Addition 9

...REPLACEMENT COUNT count

Effect

The number of search strings that have been successfully replaced is stored in the field count.

Note

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.

Example

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.

Example

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.

Addition 10

...REPLACEMENT OFFSET roff

Effect

Writes the offset of the last completely or partially replaced search string text to the file roff.

Example

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

Note

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.

Example

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



Addition 11

...REPLACEMENT LENGTH rlen

Effect

Writes the length of the last used replacement string to the field rlen.

Example

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

Exceptions

Catchable Exceptions

CX_SY_REPLACE_INFINITE_LOOP

CX_SY_RANGE_OUT_OF_BOUNDS

Related

FIND, TRANSLATE, OVERLAY

Additional help

Replacing the Contents of Fields.