ABAP offers numerous built-in functions for common operations. These functions can be used inline in expressions and make code more compact and readable.
Table Operations
line_exists() – Check if Row Exists
DATA: lt_names TYPE TABLE OF string.
lt_names = VALUE #( ( `Anna` ) ( `Bernd` ) ( `Clara` ) ).
" Check if row exists (without exception)IF line_exists( lt_names[ 2 ] ). WRITE: / 'Row 2 exists'.ENDIF.
" With keyDATA: lt_customers TYPE SORTED TABLE OF ty_customer WITH UNIQUE KEY id.
IF line_exists( lt_customers[ id = 1001 ] ). WRITE: / 'Customer 1001 found'.ENDIF.
" In conditionsDATA(lv_has_admin) = xsdbool( line_exists( lt_users[ role = 'ADMIN' ] ) ).lines() – Number of Rows
DATA: lt_items TYPE TABLE OF string.
lt_items = VALUE #( ( `A` ) ( `B` ) ( `C` ) ).
" Number of rowsDATA(lv_count) = lines( lt_items ).WRITE: / 'Count:', lv_count. " 3
" In conditionsIF lines( lt_items ) > 0. WRITE: / 'Table is not empty'.ENDIF.
" Read last rowIF lines( lt_items ) > 0. DATA(lv_last) = lt_items[ lines( lt_items ) ].ENDIF.line_index() – Index of a Row
DATA: lt_names TYPE TABLE OF string.
lt_names = VALUE #( ( `Anna` ) ( `Bernd` ) ( `Clara` ) ).
" Determine index of a rowDATA(lv_index) = line_index( lt_names[ table_line = `Bernd` ] ).WRITE: / 'Index of Bernd:', lv_index. " 2
" For sorted tables with keyDATA: lt_sorted TYPE SORTED TABLE OF ty_customer WITH UNIQUE KEY id.
DATA(lv_cust_idx) = line_index( lt_sorted[ id = 1001 ] ).Boolean Functions
xsdbool() – Boolean Conversion
DATA: lv_age TYPE i VALUE 25.
" Convert condition to booleanDATA(lv_is_adult) = xsdbool( lv_age >= 18 ).
IF lv_is_adult = abap_true. WRITE: / 'Adult'.ENDIF.
" In assignmentsDATA: lv_active TYPE abap_bool.lv_active = xsdbool( sy-subrc = 0 ).
" Complex conditionsDATA(lv_valid) = xsdbool( lv_name IS NOT INITIAL AND lv_email CS '@' AND lv_age BETWEEN 18 AND 99).boolc() – Boolean to Character
" Returns 'X' or ' 'DATA(lv_flag) = boolc( lv_age >= 18 ).
" Useful for legacy fieldsDATA: lv_legacy_flag TYPE c LENGTH 1.lv_legacy_flag = boolc( lv_condition = abap_true ).String Functions
condense() – Remove Whitespace
DATA: lv_text TYPE string VALUE ' Hello World '.
" Remove leading/trailing whitespace, reduce multiple to oneDATA(lv_condensed) = condense( lv_text ).WRITE: / lv_condensed. " 'Hello World'
" Only remove leading/trailing (NO-GAPS = FALSE)DATA(lv_trimmed) = condense( val = lv_text del = ` ` ).
" Remove all whitespaceDATA(lv_no_spaces) = condense( val = lv_text from = ` ` to = `` ).WRITE: / lv_no_spaces. " 'HelloWorld'concat_lines_of() – Table to String
DATA: lt_words TYPE TABLE OF string.
lt_words = VALUE #( ( `One` ) ( `Two` ) ( `Three` ) ).
" Join with separatorDATA(lv_sentence) = concat_lines_of( table = lt_words sep = `, ` ).WRITE: / lv_sentence. " 'One, Two, Three'
" Without separatorDATA(lv_concat) = concat_lines_of( table = lt_words ).WRITE: / lv_concat. " 'OneTwoThree'
" With line breakDATA(lv_lines) = concat_lines_of( table = lt_words sep = cl_abap_char_utilities=>newline ).to_upper() / to_lower() – Case Conversion
DATA: lv_text TYPE string VALUE 'Hello World'.
DATA(lv_upper) = to_upper( lv_text ).WRITE: / lv_upper. " 'HELLO WORLD'
DATA(lv_lower) = to_lower( lv_text ).WRITE: / lv_lower. " 'hello world'
" In comparisonsIF to_upper( lv_input ) = 'YES'. WRITE: / 'Confirmed'.ENDIF.to_mixed() / from_mixed() – CamelCase
" To CamelCaseDATA(lv_camel) = to_mixed( val = 'CUSTOMER_ORDER_ID' sep = '_' ).WRITE: / lv_camel. " 'CustomerOrderId'
" From CamelCaseDATA(lv_snake) = from_mixed( val = 'CustomerOrderId' sep = '_' ).WRITE: / lv_snake. " 'customer_order_id'shift_left() / shift_right() – Shift
DATA: lv_num TYPE string VALUE '000123'.
" Remove leading charactersDATA(lv_shifted) = shift_left( val = lv_num sub = '0' ).WRITE: / lv_shifted. " '123'
" Shift by n positionsDATA(lv_shift2) = shift_left( val = 'ABCDEF' places = 2 ).WRITE: / lv_shift2. " 'CDEF'
" Shift right (with padding)DATA(lv_right) = shift_right( val = '123' places = 5 ).reverse() – Reverse String
DATA(lv_reversed) = reverse( 'ABAP' ).WRITE: / lv_reversed. " 'PABA'escape() – Escape Characters
DATA: lv_html TYPE string VALUE '<div>Test & "Value"</div>'.
" Escape HTMLDATA(lv_escaped) = escape( val = lv_html format = cl_abap_format=>e_html_text ).WRITE: / lv_escaped. " '<div>Test & "Value"</div>'
" Escape regexDATA(lv_regex_safe) = escape( val = 'a.b*c?' format = cl_abap_format=>e_regex ).repeat() – Repeat Characters
DATA(lv_line) = repeat( val = '-' occ = 50 ).WRITE: / lv_line. " '--------------------------------------------------'
DATA(lv_spaces) = repeat( val = ` ` occ = 10 ).substring() – Substring
DATA: lv_text TYPE string VALUE 'Hello World'.
" From position 7 (0-based)DATA(lv_sub1) = substring( val = lv_text off = 6 ).WRITE: / lv_sub1. " 'World'
" From position with lengthDATA(lv_sub2) = substring( val = lv_text off = 0 len = 5 ).WRITE: / lv_sub2. " 'Hello'
" ShortcutsDATA(lv_before) = substring_before( val = lv_text sub = ' ' ). " 'Hello'DATA(lv_after) = substring_after( val = lv_text sub = ' ' ). " 'World'DATA(lv_from) = substring_from( val = lv_text sub = 'Wo' ). " 'World'DATA(lv_to) = substring_to( val = lv_text sub = 'o ' ). " 'Hello 'strlen() / numofchar() – String Length
DATA: lv_text TYPE string VALUE 'Hello'.
DATA(lv_len) = strlen( lv_text ).WRITE: / 'Length:', lv_len. " 5
" For any character typesDATA: lv_char TYPE c LENGTH 10 VALUE 'Test'.DATA(lv_numchar) = numofchar( lv_char ). " 4 (without trailing spaces)contains() / matches() – Search
" Contains substring?IF contains( val = lv_email sub = '@' ). WRITE: / 'Valid email (contains @)'.ENDIF.
" Starts with?IF contains( val = lv_email start = 'test' ). WRITE: / 'Starts with test'.ENDIF.
" Ends with?IF contains( val = lv_email end = '.com' ). WRITE: / 'Ends with .com'.ENDIF.
" Regex matchIF matches( val = lv_email regex = '^\w+@\w+\.\w+$' ). WRITE: / 'Email format correct'.ENDIF.find() / count() – Find and Count
DATA: lv_text TYPE string VALUE 'ABAP is great, ABAP is awesome'.
" Find positionDATA(lv_pos) = find( val = lv_text sub = 'is' ).WRITE: / 'Position:', lv_pos. " 5
" Count all occurrencesDATA(lv_count) = count( val = lv_text sub = 'ABAP' ).WRITE: / 'Count:', lv_count. " 2
" With regexDATA(lv_regex_count) = count( val = lv_text regex = '\bis\b' ).replace() – Replace
DATA: lv_text TYPE string VALUE 'Hello World'.
" Replace first occurrenceDATA(lv_new1) = replace( val = lv_text sub = 'World' with = 'ABAP' ).WRITE: / lv_new1. " 'Hello ABAP'
" Replace all occurrencesDATA: lv_multi TYPE string VALUE 'a-b-c-d'.DATA(lv_new2) = replace( val = lv_multi sub = '-' with = '_' occ = 0 ).WRITE: / lv_new2. " 'a_b_c_d'
" With regexDATA(lv_digits) = replace( val = 'abc123def' regex = '\d+' with = 'XXX' ).WRITE: / lv_digits. " 'abcXXXdef'segment() – Split String
DATA: lv_path TYPE string VALUE '/usr/local/bin/app'.
" Extract segment (1-based)DATA(lv_seg1) = segment( val = lv_path index = 1 sep = '/' ). " ''DATA(lv_seg2) = segment( val = lv_path index = 2 sep = '/' ). " 'usr'DATA(lv_seg3) = segment( val = lv_path index = -1 sep = '/' ). " 'app' (last)Numeric Functions
abs() – Absolute Value
DATA(lv_abs) = abs( -42 ).WRITE: / lv_abs. " 42sign() – Sign
DATA(lv_sign1) = sign( -5 ). " -1DATA(lv_sign2) = sign( 0 ). " 0DATA(lv_sign3) = sign( 10 ). " 1ceil() / floor() / trunc() – Rounding
DATA: lv_num TYPE p DECIMALS 2 VALUE '3.7'.
DATA(lv_ceil) = ceil( lv_num ). " 4 (round up)DATA(lv_floor) = floor( lv_num ). " 3 (round down)DATA(lv_trunc) = trunc( lv_num ). " 3 (truncate)round() – Round
DATA: lv_num TYPE p DECIMALS 4 VALUE '3.14159'.
DATA(lv_rounded) = round( val = lv_num dec = 2 ).WRITE: / lv_rounded. " 3.14
" Round to multipleDATA(lv_round_5) = round( val = 127 dec = 0 prec = 5 ). " 125nmin() / nmax() – Minimum/Maximum
DATA(lv_min) = nmin( val1 = 10 val2 = 5 ).WRITE: / 'Min:', lv_min. " 5
DATA(lv_max) = nmax( val1 = 10 val2 = 5 ).WRITE: / 'Max:', lv_max. " 10
" More than 2 valuesDATA(lv_max3) = nmax( val1 = 10 val2 = 25 val3 = 15 ).WRITE: / 'Max:', lv_max3. " 25ipow() – Power (Integer)
DATA(lv_power) = ipow( base = 2 exp = 10 ).WRITE: / lv_power. " 1024frac() – Fractional Part
DATA: lv_num TYPE p DECIMALS 2 VALUE '3.75'.
DATA(lv_frac) = frac( lv_num ).WRITE: / lv_frac. " 0.75Date/Time Functions
utclong_current() – Current Timestamp
DATA(lv_timestamp) = utclong_current( ).WRITE: / lv_timestamp. " 2024-11-24 10:30:00.1234567utclong_add() – Add Time
DATA(lv_now) = utclong_current( ).
" Add 1 dayDATA(lv_tomorrow) = utclong_add( val = lv_now days = 1).
" Add 2 hoursDATA(lv_later) = utclong_add( val = lv_now hours = 2).utclong_diff() – Time Difference
DATA: lv_start TYPE utclong, lv_end TYPE utclong.
" Difference in secondsDATA(lv_seconds) = utclong_diff( high = lv_end low = lv_start).Type Functions
cl_abap_typedescr – Type Information
DATA: lv_string TYPE string VALUE 'Test'.
" Determine type nameDATA(lo_type) = cl_abap_typedescr=>describe_by_data( lv_string ).WRITE: / 'Type:', lo_type->get_relative_name( ). " 'STRING'WRITE: / 'Kind:', lo_type->kind. " 'E' (Elementary)Overview by Category
| Category | Functions |
|---|---|
| Tables | lines(), line_exists(), line_index() |
| Boolean | xsdbool(), boolc() |
| String manipulation | condense(), to_upper(), to_lower(), to_mixed(), from_mixed() |
| String search | contains(), matches(), find(), count() |
| String extraction | substring(), substring_before(), substring_after(), segment() |
| String misc | concat_lines_of(), escape(), repeat(), reverse(), replace(), strlen() |
| Numeric | abs(), sign(), ceil(), floor(), trunc(), round(), nmin(), nmax(), ipow(), frac() |
| Date/Time | utclong_current(), utclong_add(), utclong_diff() |
Important Notes / Best Practice
- Built-in functions are performant – prefer them over custom implementations.
line_exists()avoids exceptions on table access – better than TRY-CATCH.xsdbool()returnsabap_true/abap_false– ideal for boolean expressions.condense()replacesCONDENSEstatement in expressions.concat_lines_of()replaces complex LOOP constructs for string concatenation.to_upper()/to_lower()are functions –TRANSLATE ... TO UPPER/LOWERis a statement.- Combine with
String Templatesfor readable formatting. nmin()/nmax()support up to 9 values.matches()for regex checks,contains()for simple substring search.- Use
CONVwhen type conversion is needed.