ABAP String templates Examples

 After talking about string templates in the last post String processing using string templates, I was tempted to provide more examples of just how powerful and simple to use string templates can be. so without much writing, here we go.

Adding Whitespaces and Comments:

report syst-repid.
DATA:
lv_String TYPE string.
START-OF-SELECTION.
lv_String = |line 1;\n| & " the 1st line
* This is a comment ,, the string template still continues
| line{ 1 + 1 }| &
* The previous line still continues.
| (still line { 1 + 1 });\n| &
| line { 1 + 2 };|.
cl_demo_text=>show_string( lv_String ).

Control and escape Characters: 

report syst-repid.
DATA:
lv_String TYPE string.
DATA:
i1 TYPE i.
START-OF-SELECTION.
lv_String = |Printing Linebreak:\n.Printing Tab: \t. Printing Reserved Characters: \\, \{, \}.|.
cl_demo_text=>show_string( lv_String ).

Format Options: 

    Width, Alignment, Padding, Zero, Case

report syst-repid.
DATA:
lv_String TYPE cl_demo_text=>t_line.
DATA:
lv_int TYPE i,
lv_float TYPE f,
lv_txt TYPE string.
DATA:
lo_txt TYPE REF TO cl_demo_text.
START-OF-SELECTION.
lo_txt = cl_demo_text=>get_handle( ).
lv_String = |[{ 12 WIDTH = 5 ALIGN = LEFT }]| &
|[{ 12 WIDTH = 5 ALIGN = CENTER }]| &
|[{ 12 WIDTH = 5 ALIGN = (cl_abap_format=>a_RIGHT) }]| &
|[{ lv_int WIDTH = 5 ZERO = NO }]| &
|[{ lv_int WIDTH = 5 ZERO = YES }]|.
lo_txt->add_line( lv_String ).
lv_String = |[{ 12 WIDTH = 5 ALIGN = LEFT PAD = '.' }]| &
|[{ 12 WIDTH = 5 ALIGN = CENTER PAD = '.' }]| &
|[{ 12 WIDTH = 5 ALIGN = RIGHT PAD = '_' }]| &
|[{ lv_float WIDTH = 5 PAD = '_' ZERO = NO }]| &
|[{ lv_float WIDTH = 5 PAD = '_' ZERO = YES }]|.
lo_txt->add_line( lv_String ).
lv_txt = |I am YELLING.|.
lv_String = |[ { lv_txt } ] [{ lv_txt CASE = LOWER }]|.
lo_txt->add_line( lv_String ).
lo_txt->display( ).

    Sign, Decimals, Exponent, Currency, Number, Style:

report syst-repid.
DATA:
lv_String TYPE cl_demo_text=>t_line.
DATA:
lv_int TYPE i,
lv_float TYPE f,
lv_txt TYPE string.
DATA:
lo_txt TYPE REF TO cl_demo_text.
START-OF-SELECTION.
lv_float = '0.75'.
lv_int = 55.
lo_txt = cl_demo_text=>get_handle( ).
lv_String = |Left: [{ lv_float SIGN = LEFT }] [{ lv_float SIGN = LEFTPLUS }] [{ lv_float SIGN = LEFTSPACE }]| &
|Right: [{ lv_int SIGN = RIGHT }] [{ lv_int SIGN = RIGHTPLUS }] [{ lv_int SIGN = RIGHTSPACE }]|.
lo_txt->add_line( lv_String ).
lv_String = |DECIMALS:[{ lv_int DECIMALS = 1 }] [{ lv_float DECIMALS = 1 EXPONENT = -3 }]|.
lo_txt->add_line( lv_String ).
"Currency
lv_String = |CURRENCY:[{ lv_int CURRENCY = 'EUR' }][{ lv_float CURRENCY = 'USD' }]|.
lo_txt->add_line( lv_String ).
"Decimal separators
lv_int = 123456789. lv_float = '1234.0625'.
lv_String = |NUMBER:[{ lv_int NUMBER = RAW }] [{ lv_int NUMBER = USER }]| &
| [{ lv_float NUMBER = RAW }] [{ lv_float NUMBER = (cl_abap_format=>n_USER) }]|.
lo_txt->add_line( lv_String ).
"style (as in WRITE)
lv_float = '-2.345'.
lv_String = |STYLE:[{ lv_float STYLE = SIMPLE }] [{ lv_float STYLE = SCIENTIFIC }]| &
| [{ lv_float STYLE = SIGN_AS_POSTFIX }]|.
lo_txt->display( ).

    Date, Time, Timestamp, Timezone:

report syst-repid.
DATA:
lv_String TYPE cl_demo_text=>t_line.
DATA:
lv_Date TYPE sydatum,
lv_time TYPE syuzeit,
lv_timestamp TYPE timestampl,
lv_timezone TYPE timezone.
DATA:
lo_txt TYPE REF TO cl_demo_text.
START-OF-SELECTION.
lo_txt = cl_demo_text=>get_handle( ).
"Date
lv_date = syst-datum.
lv_String = |DATE:{ lv_date DATE = RAW } { lv_date DATE = ISO } { lv_date DATE = USER }|.
lo_txt->add_line( lv_String ).
"Time
lv_time = sy-uzeit.
lv_String = |TIME:{ lv_time TIME = RAW } { lv_time TIME = (cl_abap_format=>t_ISO) }|.
lo_txt->add_line( lv_String ).
"Timestamp
GET TIME STAMP FIELD lv_timestamp. lv_timezone = 'UTC+5'.
lv_String = |Timestamp: { lv_timestamp TIMEZONE = lv_timezone TIMESTAMP = SPACE }\n| &
|{ lv_timestamp TIMEZONE = lv_timezone TIMESTAMP = ISO }\n| &
|{ lv_timestamp TIMEZONE = lv_timezone TIMESTAMP = ENVIRONMENT }|.
lo_txt->add_line( lv_String ).
lo_txt->display( ).
Hope you liked the code snippets and examples.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP