ABAP String Processing
String handling in ABAP has evolved significantly over the years. Understanding the difference between
STRING and CHAR(n) types, and knowing
both legacy and modern syntax, is essential for writing correct and maintainable ABAP code.
This guide covers the most common string operations with runnable examples.
String Types in ABAP
ABAP has two fundamentally different string types: STRING is a dynamic-length
heap-allocated string, while CHAR(n) is a fixed-length character field stored
inline. Using STRING where CHAR(n) is expected
(e.g. a database field) causes implicit conversions and potential truncation bugs.
Choose the type that matches your use case and the target field definition.
" Fixed-length: trailing spaces are significant, padded to length
DATA lv_char TYPE c LENGTH 20. " or: CHAR20 / char20
lv_char = 'Hello'. " stored as 'Hello '
" Dynamic-length: no padding, heap-allocated
DATA lv_string TYPE string.
lv_string = 'Hello'. " stored as 'Hello' (5 chars)
Concatenation with &&
Since ABAP 7.02, you can use the && operator to concatenate strings inline,
just like + in Python or Java. This is much more readable than the legacy
CONCATENATE statement and works naturally in assignments and expressions.
String templates with |...| syntax are even more powerful for complex formatting.
DATA: lv_first TYPE string VALUE 'John',
lv_last TYPE string VALUE 'Doe',
lv_result TYPE string.
" Using && operator (modern, recommended)
lv_result = lv_first && ' ' && lv_last.
WRITE: / lv_result. " John Doe
" Using string template (even more powerful)
lv_result = |{ lv_first } { lv_last }|.
WRITE: / lv_result. " John Doe
CONCATENATE Statement (Legacy)
The legacy CONCATENATE statement joins multiple strings into a target variable
and optionally inserts a separator with SEPARATED BY.
You will encounter this pattern frequently in existing ABAP codebases, so knowing how to read it is important.
For new code, prefer the && operator or string templates instead.
DATA: lv_city TYPE string VALUE 'Tokyo',
lv_country TYPE string VALUE 'Japan',
lv_result TYPE string.
" Legacy style (still valid, commonly found in older systems)
CONCATENATE lv_city ', ' lv_country INTO lv_result.
WRITE: / lv_result. " Tokyo, Japan
" With SEPARATED BY
CONCATENATE lv_city lv_country INTO lv_result SEPARATED BY ', '.
WRITE: / lv_result. " Tokyo, Japan
String Length with STRLEN()
STRLEN() returns the number of characters in a string, not counting trailing
spaces for CHAR fields. It is the standard function for getting string length
in ABAP and works on both STRING and CHAR(n) types.
Use it for validation, substring operations, and any length-dependent logic.
DATA: lv_text TYPE string VALUE 'Hello, ABAP!',
lv_length TYPE i.
lv_length = STRLEN( lv_text ).
WRITE: / 'Length:', lv_length. " Length: 12
" Useful for conditional checks
IF STRLEN( lv_text ) > 10.
WRITE: / 'String is longer than 10 characters.'.
ENDIF.
Case Conversion with TRANSLATE
The TRANSLATE statement converts a string to upper or lower case in place.
It modifies the variable directly rather than returning a new value, which is a common gotcha for developers
coming from other languages. For read-only conversions without side effects, use the
to_upper / to_lower string functions available in ABAP 7.40+.
DATA lv_text TYPE string VALUE 'Hello World'.
" TRANSLATE modifies the variable in place
TRANSLATE lv_text TO UPPER CASE.
WRITE: / lv_text. " HELLO WORLD
TRANSLATE lv_text TO LOWER CASE.
WRITE: / lv_text. " hello world
" Modern alternative (ABAP 7.40+): expression-style, no side effects
DATA lv_upper TYPE string.
lv_upper = to_upper( lv_text ).
WRITE: / lv_upper. " HELLO WORLD
WRITE: / lv_text. " hello world (unchanged)
Full Example
This complete example combines all the string operations covered in this guide. Paste it into ABAP Dojo and click Run to see each technique in action.
REPORT zstring_processing.
DATA: lv_first TYPE string VALUE 'John',
lv_last TYPE string VALUE 'Doe',
lv_full TYPE string,
lv_length TYPE i,
lv_upper TYPE string.
" Concatenation with &&
lv_full = lv_first && ' ' && lv_last.
WRITE: / 'Full name:', lv_full.
" String template
WRITE: / |Hello, { lv_full }!|.
" Length
lv_length = STRLEN( lv_full ).
WRITE: / 'Name length:', lv_length.
" Upper case
lv_upper = lv_full.
TRANSLATE lv_upper TO UPPER CASE.
WRITE: / 'Upper case:', lv_upper.
" Legacy CONCATENATE with SEPARATED BY
DATA: lv_city TYPE string VALUE 'Berlin',
lv_country TYPE string VALUE 'Germany',
lv_location TYPE string.
CONCATENATE lv_city lv_country INTO lv_location SEPARATED BY ', '.
WRITE: / 'Location:', lv_location.