Replace Tab with space in SAP ABAP

In SAP ABAP, replacing tab characters with spaces is a common requirement when processing text data. You just need to make a small change by adding an "If condition" for handling the tab character as shown below ?

Method

To replace tab characters with spaces, you need to check if the field contains the tab character using the CO (Contains Only) operator and a hexadecimal constant ?

if <field_name> CO gc_hex_char

Complete Example

Here's a more complete implementation showing how to replace tab characters with spaces ?

DATA: lv_text TYPE string,
      gc_tab_char TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

" Sample text with tab character
lv_text = 'Hello' && gc_tab_char && 'World'.

" Check and replace tab with space
IF lv_text CA gc_tab_char.
  REPLACE ALL OCCURRENCES OF gc_tab_char IN lv_text WITH ' '.
ENDIF.

WRITE: / 'Processed text:', lv_text.

In this example:

  • cl_abap_char_utilities=>horizontal_tab provides the tab character constant
  • CA (Contains Any) operator checks if the string contains the tab character
  • REPLACE ALL OCCURRENCES replaces all tab characters with spaces

Alternative Approach

You can also use the TRANSLATE statement for a more direct approach ?

TRANSLATE lv_text USING cl_abap_char_utilities=>horizontal_tab && ' '.

Conclusion

This approach effectively handles tab replacement without hardcoding character values, making it a clean and maintainable implementation for text processing in SAP ABAP.

Updated on: 2026-03-13T18:52:12+05:30

735 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements