String Processing & String Templates

String templates are enclosed in |…|; expressions go in { }:

8.1 String templates (7.02+)

String templates are enclosed in |…|; expressions go in { }:

Chaining: the && operator concatenates character strings:

DATA(lv_name) = `Rachel`.
DATA(lv_msg)  = |Hello { lv_name }, you have { lines( lt_orders ) } orders.|.

" Formatting options
DATA(lv_amt) = |{ lv_amount NUMBER = USER }|.          " user's decimal format
DATA(lv_dt)  = |{ sy-datum DATE = ISO }|.              " 2025-06-30
DATA(lv_tm)  = |{ sy-uzeit TIME = USER }|.
DATA(lv_pad) = |{ lv_matnr ALPHA = OUT }|.             " remove leading zeros
DATA(lv_in)  = |{ '123' ALPHA = IN WIDTH = 10 }|.      " 0000000123
DATA(lv_w)   = |{ lv_text WIDTH = 20 ALIGN = RIGHT PAD = '.' }|.
DATA(lv_cs)  = |{ lv_text CASE = UPPER }|.
DATA(lv_cur) = |{ lv_amount CURRENCY = lv_waers }|.
DATA(lv_nl)  = |Line 1\nLine 2|.                       " escapes: \n \t \\ \| \{ \}
lv_full = lv_first && ` ` && lv_last.
lv_full = |{ lv_first } { lv_last }|.       " same result with a template

8.2 Classic string statements

CONCATENATE a b INTO c SEPARATED BY space. SPLIT lv_csv AT ',' INTO TABLE lt_parts. CONDENSE lv_text [NO-GAPS]. Remove extra spaces TRANSLATE lv_text TO UPPER CASE.

REPLACE ALL OCCURRENCES OF 'a' IN lv_text WITH 'b'. FIND FIRST OCCURRENCE OF 'x' IN lv_text MATCH OFFSET lv_off.

SHIFT lv_text LEFT DELETING LEADING '0'. OVERLAY, WRITE … TO strlen( lv_text)

Length (ignores trailing spaces for c)

8.3 Built-in string functions (7.02+)

DATA(s) = `  ABAP is fun  `.
condense( s )                                      " "ABAP is fun"
to_upper( s )  to_lower( s )  to_mixed( `hello_world` )  from_mixed( `HelloWorld` )
strlen( s )                                        " length
substring( val = s off = 2 len = 4 )               " "ABAP"
substring_after( val = `key=value` sub = `=` )     " "value"
substring_before( val = `key=value` sub = `=` )    " "key"
find( val = s sub = `is` )                         " offset or -1
count( val = s sub = `a` case = abap_false )
replace( val = s sub = `fun` with = `great` occ = 0 )   " occ = 0: all occurrences
reverse( `abc` )                                   " "cba"
shift_left( val = `0012` sub = `0` )               " "12"
repeat( val = `-` occ = 20 )
segment( val = `a;b;c` index = 2 sep = `;` )       " "b"
concat_lines_of( table = lt_words sep = ` ` )
contains( val = s sub = `ABAP` )                   " predicate function
matches( val = lv_email regex = `^[^@]+@[^@]+\.[a-z]+$` )
escape( val = lv_html format = cl_abap_format=>e_xss_ml )

8.4 Offset/length access

DATA lv_date TYPE d VALUE '20250630'.
DATA(lv_year)  = lv_date(4).       " 2025
DATA(lv_month) = lv_date+4(2).     " 06
DATA(lv_day)   = lv_date+6(2).     " 30

8.5 Regular expressions

ABAP supports PCRE (7.55+, recommended) and POSIX regexes (obsolete):

FIND ALL OCCURRENCES OF PCRE `\d+` IN lv_text RESULTS DATA(lt_matches).
REPLACE ALL OCCURRENCES OF PCRE `\s+` IN lv_text WITH ` `.
DATA(lo_matcher) = cl_abap_regex=>create_pcre( pattern = `(\w+)@(\w+)` )->create_matcher( text = lv_text ).
WHILE lo_matcher->find_next( ).
  DATA(lv_user) = lo_matcher->get_submatch( 1 ).
ENDWHILE.

8.6 Text symbols and messages

Text symbols (TEXT-001) and selection texts are translatable (Goto → Text Elements).

Messages (SE91, message class ZMSG):

Message types: A abort, E error, W warning, I info, S success, X exit (short dump).

MESSAGE e001(zmsg) WITH lv_order.          " error message 001 with placeholder &1
MESSAGE 'Saved' TYPE 'S'.                  " success
MESSAGE i002(zmsg) INTO DATA(lv_dummy).    " fill sy-msg* without displaying