ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Character String and Byte String Processing → Statements for Character String and Byte String Processing
REPLACE
Other versions: 7.31 | 7.40 | 7.54
Syntax Forms
Pattern-Based Replacement
1. REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]
pattern
IN [section_of] dobj WITH new
[IN {CHARACTER|BYTE} MODE]
[replace_options].
Position-Based Replacement
2. REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new
[IN {CHARACTER|BYTE} MODE].
Effect
This statement replaces characters or bytes of the variable dobj
with characters or bytes of the operand new
. This operand is a
character-like expression position. Here, there is a difference between pattern-based and position-based replacements.
When the replacement is performed, an intermediate result without a length limit is created implicitly
and passed to the data object dobj
. If the length of the intermediate result
is longer than the length of dobj
, the object is truncated on the right in
the case of data objects of fixed length. If the length of the intermediate result is shorter than the
length of dobj
, data objects of fixed length are padded on the right with
blanks or hexadecimal zeros. Data objects of variable length are adjusted accordingly. If data is truncated on the right when the intermediate result is assigned, sy-subrc
is set to 2.
When the character string is processed, the trailing blanks are respected for data objects dobj
of fixed length. For new
the closing blanks are ignored.
Example
The simplest form of the statement REPLACE
.
DATA(str) = `Hallo`.
REPLACE `a` IN str WITH `e`.
cl_demo_output=>display( str ).
System Fields
sy-subrc | Meaning |
---|---|
0 | The search pattern or specified section was replaced by the content of new and the result is available in full in dobj . |
2 | The search pattern or specified section was replaced by the content of new in dobj and the result of the replacement was truncated on the right. |
4 | The search pattern in pattern was not found in dobj in the pattern-based search. |
Notes
-
The statement
REPLACE IN TABLE
can be used to make replacements in internal tables. -
These forms of the statement
REPLACE
replace the following obsolete form:
REPLACE substring WITH new INTO dobj ...
-
To replace parts of a character string in an operand position, a
replace function can be used that includes some of the functions of the statement
REPLACE
.