Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables 

REPLACE IN TABLE itab

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]
pattern 
        IN TABLE itab [table_range] WITH new
        [IN {CHARACTER|BYTE} MODE]
        [replace_options].

Effect

The internal table itab is scanned row-by-row for the character or byte strings specified by pattern and replaces any occurrences with the content of the operand new. new is a character-like expression position.

itab expects a standard table without secondary table keys. The rows in the table must be character-like or byte-like, depending on the addition CHARACTER or BYTE MODE. Byte or character strings that cover multiple table rows are not replaced.

The addition table_range can be used to restrict the search range in the table. When searching in individual table rows, the other additions operate in the same way as in the statement REPLACE pattern IN for elementary character or byte strings, with an extra addition, REPLACEMENT LINE, returning the row number of an occurrence.

In string processing for row types of fixed length, trailing blanks are respected, whereas with new they are ignored.


Notes

  • REPLACE IN TABLE can be used in tables with structured row types to replace character strings if the structure only contains flat character-like components. Every row is then handled in the same way as a field of type c.
  • Replacements using REPLACE IN TABLE give better performance than running a LOOP and using REPLACE to make replacements in individual rows.

System Fields

sy-subrc Meaning
0 The search string was replaced by the content of new and the full result is available in the table row(s).
2 The search pattern was replaced by the content of new and the result of the replacement was truncated on the right in at least one table row.
4 The search pattern in pattern was not found in the internal table.
8 The operands pattern or new do not contain interpretable double-byte characters.

The values of sy-tabix and sy-fdpos are not changed.


Example

A simple "DM-Euro Conversion".

DATA menu TYPE TABLE OF string WITH EMPTY KEY. 

menu = value #( ( `Beer  -  3 DM` ) 
                ( `Pizza - 10 DM` ) ). 

REPLACE ALL OCCURRENCES OF REGEX `\b(DM)\b` 
        IN TABLE menu WITH 'EUR' 
        RESPECTING CASE. 

Exceptions

Handleable Exceptions

CX_SY_REPLACE_INFINITE_LOOP

  • Cause: Substring of length 0 creates an endless loop when searching for all occurrences.
    Runtime error: REPLACE_INFINITE_LOOP

CX_SY_TAB_RANGE_OUT_OF_BOUNDS

  • Cause: Illegal offset or row specification in the addition of FROM ... OFFSET ... TO OFFSET.
    Runtime error: INVALID_TABLE_RANGE

CX_SY_INVALID_REGEX

  • Cause: Invalid expression after the addition REGEX.
    Runtime error: INVALID_REGEX

Continue

REPLACE IN TABLE - table_range

REPLACE IN TABLE - options