Truncating multiple strings after 100 characters in ABAP

You can truncate strings to 100 characters in ABAP by defining a character field of 100 bytes and moving your variable to that character field. When you move a longer string to a shorter field, ABAP automatically truncates it to fit the target field length.

Example

The following example demonstrates how to truncate a string to 100 characters using a fixed-length character field ?

DATA: lv_original_text TYPE string VALUE 'This is a very long text that exceeds 100 characters and will be truncated when moved to the 100-character field defined below for demonstration purposes.',
      lv_truncated_text(100) TYPE c.

" Move the long text to the 100-character field (automatic truncation)
lv_truncated_text = lv_original_text.

" Display the truncated text
WRITE: / 'Original length:', strlen( lv_original_text ),
       / 'Truncated text:', lv_truncated_text,
       / 'Truncated length:', strlen( lv_truncated_text ).

The output of the above code is ?

Original length: 141
Truncated text: This is a very long text that exceeds 100 characters and will be truncated when moved to th
Truncated length: 100

Truncating Multiple Strings

To truncate multiple strings, you can use the same approach in a loop or process each string individually ?

DATA: lv_temp_text(100) TYPE c,
      lt_long_texts TYPE TABLE OF string,
      lt_short_texts TYPE TABLE OF string,
      lv_text TYPE string.

" Fill table with long texts
APPEND 'This is the first very long string that needs to be truncated to exactly 100 characters for processing' TO lt_long_texts.
APPEND 'This is the second extremely long string that also exceeds the 100 character limit and requires truncation' TO lt_long_texts.
APPEND 'Short text' TO lt_long_texts.

" Process each string
LOOP AT lt_long_texts INTO lv_text.
  lv_temp_text = lv_text.  " Automatic truncation occurs here
  APPEND lv_temp_text TO lt_short_texts.
ENDLOOP.

" Display results
LOOP AT lt_short_texts INTO lv_text.
  WRITE: / lv_text.
ENDLOOP.

Conclusion

Truncating strings in ABAP is straightforward using fixed-length character fields, as the system automatically handles the truncation when moving data between fields of different lengths. This method works efficiently for both single strings and multiple strings processed in loops.

Updated on: 2026-03-13T18:45:34+05:30

604 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements