abap news
Home » ABAP replace( ): Fix Only the First Occurrence Changing

ABAP replace( ): Fix Only the First Occurrence Changing

ABAPBy xabap Published
Three circles become three blue diamonds, illustrating replacement of every occurrence.

If ABAP replace( ) changes only the first match, add occ = 0 to replace all occurrences. The default is occ = 1; a positive value selects one numbered occurrence rather than setting how many replacements to perform.

Why the remaining matches survive

Consider a generated identifier containing three hyphens. Calling `replace( val = original sub = – with = _ )` without an occurrence parameter changes only its first match. With the sample input below, the call succeeds, so this is a result error rather than an exception.

The parameter occ identifies the occurrence to replace: 2 means only the second match, while 0 means every match. SAP documents this special meaning in its [string function parameter lesson](https://learning.sap.com/courses/deepening-your-abap-programming-knowledge/processing-strings-using-functions-and-regular-expressions_abe828f3-4e84-45ce-9c39-0cf9eb2a9f2f).

This technique applies to Standard ABAP and ABAP Cloud. The example uses an ADT console class with IF_OO_ADT_CLASSRUN; it needs no database objects or application data.

Compare the default call with the fix

Create global class ZCL_REPLACE_OCC_DEMO in ADT, paste the complete implementation, activate it, and run it as an ABAP Application (Console). Every call reads the same original value, so earlier replacements cannot affect later results.

The function returns a new string. Assigning that result to a separate variable preserves the input, as shown in [SAP’s replacement examples](https://github.com/SAP-samples/abap-cheat-sheets/blob/main/07_String_Processing.md#string-function-replace).

CLASS zcl_replace_occ_demo DEFINITION
  PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_replace_occ_demo IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA original TYPE string VALUE `EU-2026-INV-0042`.
    DATA first_only TYPE string.
    DATA second_only TYPE string.
    DATA every_match TYPE string.

    first_only = replace(
      val = original sub = `-` with = `_` ).

    second_only = replace(
      val = original sub = `-` with = `_`
      occ = 2 ).

    every_match = replace(
      val = original sub = `-` with = `_`
      occ = 0 ).

    out->write( original ).
    out->write( first_only ).
    out->write( second_only ).
    out->write( every_match ).
  ENDMETHOD.
ENDCLASS.

Derive the expected output

The input contains three hyphens, after EU, 2026, and INV. The default call replaces the first; occ = 2 replaces only the second; occ = 0 replaces all three. Each replacement exchanges one character for one character.

The four console lines should therefore appear in this order. These results are derived from the code; the example has not been executed here.

Output variableExpected value
originalEU-2026-INV-0042
first_onlyEU_2026-INV-0042
second_onlyEU-2026_INV-0042
every_matchEU_2026_INV_0042

Limits and when to keep a single replacement

Do not use occ = 0 when only one separator or placeholder should change. Keep the default for the first match, or select the required occurrence explicitly. Searches are case-sensitive by default, which matters when replacing words instead of punctuation.

Keep the receiving variable typed as string when the replacement can increase its length. A fixed-length destination can lose characters during assignment.

If you want to modify an existing variable directly, the statement form is REPLACE ALL OCCURRENCES OF ... IN ... WITH .... SAP describes its assignment and truncation behavior in [Replacements in Character Strings](https://help.sap.com/saphelp_gbt10/helpdata/en/9f/db997b35c111d1829f0000e829fbfe/content.htm?no_cache=true). Use the function when you need the returned text inside an expression or want to preserve the input.

Sources: SAP Learning: String function parameters and occurrence selection · SAP ABAP Cheat Sheets: String processing and replacement examples · SAP Library: Replacements in character strings