Jul 6, 2026

Replace Some Characters in the Middle of Text.

Requirement:

To replace the 5th character in a 10-character string (or any string), use the ABAP overlay statement or the replace section command. In ABAP, character offsets are zero-based, meaning the 5th character is at offset

Solution:

Here are the two best approaches for doing this:

Method 1: Using OVERLAY (Best for replacing specific offsets)
abap
DATA: gv_string TYPE string VALUE '1234567890'.

" The 5th character is at offset 4 (length 1). Replace it with 'X'
OVERLAY gv_string WITH '    X' ONLY ' '.

Method 2: Using REPLACE SECTION (Best for explicit length)
abap
DATA: gv_string TYPE string VALUE '1234567890'.

REPLACE SECTION OFFSET 4 LENGTH 1 OF gv_string WITH 'X'.

Method 3: Inline Expression (ABAP 7.40+)
abap
DATA(gv_string) = '1234567890'.

gv_string = |{ gv_string(4) }X{ gv_string+5 }|.

Output for all methods: 1234X67890

No comments :