ABAP Built-in Functions: Practical Helper Functions

Category
ABAP-Statements
Published
Author
Johannes

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 key
DATA: 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 conditions
DATA(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 rows
DATA(lv_count) = lines( lt_items ).
WRITE: / 'Count:', lv_count. " 3
" In conditions
IF lines( lt_items ) > 0.
WRITE: / 'Table is not empty'.
ENDIF.
" Read last row
IF 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 row
DATA(lv_index) = line_index( lt_names[ table_line = `Bernd` ] ).
WRITE: / 'Index of Bernd:', lv_index. " 2
" For sorted tables with key
DATA: 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 boolean
DATA(lv_is_adult) = xsdbool( lv_age >= 18 ).
IF lv_is_adult = abap_true.
WRITE: / 'Adult'.
ENDIF.
" In assignments
DATA: lv_active TYPE abap_bool.
lv_active = xsdbool( sy-subrc = 0 ).
" Complex conditions
DATA(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 fields
DATA: 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 one
DATA(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 whitespace
DATA(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 separator
DATA(lv_sentence) = concat_lines_of( table = lt_words sep = `, ` ).
WRITE: / lv_sentence. " 'One, Two, Three'
" Without separator
DATA(lv_concat) = concat_lines_of( table = lt_words ).
WRITE: / lv_concat. " 'OneTwoThree'
" With line break
DATA(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 comparisons
IF to_upper( lv_input ) = 'YES'.
WRITE: / 'Confirmed'.
ENDIF.

to_mixed() / from_mixed() – CamelCase

" To CamelCase
DATA(lv_camel) = to_mixed( val = 'CUSTOMER_ORDER_ID' sep = '_' ).
WRITE: / lv_camel. " 'CustomerOrderId'
" From CamelCase
DATA(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 characters
DATA(lv_shifted) = shift_left( val = lv_num sub = '0' ).
WRITE: / lv_shifted. " '123'
" Shift by n positions
DATA(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 HTML
DATA(lv_escaped) = escape( val = lv_html format = cl_abap_format=>e_html_text ).
WRITE: / lv_escaped. " '&lt;div&gt;Test &amp; &quot;Value&quot;&lt;/div&gt;'
" Escape regex
DATA(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 length
DATA(lv_sub2) = substring( val = lv_text off = 0 len = 5 ).
WRITE: / lv_sub2. " 'Hello'
" Shortcuts
DATA(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 types
DATA: lv_char TYPE c LENGTH 10 VALUE 'Test'.
DATA(lv_numchar) = numofchar( lv_char ). " 4 (without trailing spaces)
DATA: lv_email TYPE string VALUE '[email protected]'.
" 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 match
IF 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 position
DATA(lv_pos) = find( val = lv_text sub = 'is' ).
WRITE: / 'Position:', lv_pos. " 5
" Count all occurrences
DATA(lv_count) = count( val = lv_text sub = 'ABAP' ).
WRITE: / 'Count:', lv_count. " 2
" With regex
DATA(lv_regex_count) = count( val = lv_text regex = '\bis\b' ).

replace() – Replace

DATA: lv_text TYPE string VALUE 'Hello World'.
" Replace first occurrence
DATA(lv_new1) = replace( val = lv_text sub = 'World' with = 'ABAP' ).
WRITE: / lv_new1. " 'Hello ABAP'
" Replace all occurrences
DATA: 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 regex
DATA(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. " 42

sign() – Sign

DATA(lv_sign1) = sign( -5 ). " -1
DATA(lv_sign2) = sign( 0 ). " 0
DATA(lv_sign3) = sign( 10 ). " 1

ceil() / 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 multiple
DATA(lv_round_5) = round( val = 127 dec = 0 prec = 5 ). " 125

nmin() / 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 values
DATA(lv_max3) = nmax( val1 = 10 val2 = 25 val3 = 15 ).
WRITE: / 'Max:', lv_max3. " 25

ipow() – Power (Integer)

DATA(lv_power) = ipow( base = 2 exp = 10 ).
WRITE: / lv_power. " 1024

frac() – Fractional Part

DATA: lv_num TYPE p DECIMALS 2 VALUE '3.75'.
DATA(lv_frac) = frac( lv_num ).
WRITE: / lv_frac. " 0.75

Date/Time Functions

utclong_current() – Current Timestamp

DATA(lv_timestamp) = utclong_current( ).
WRITE: / lv_timestamp. " 2024-11-24 10:30:00.1234567

utclong_add() – Add Time

DATA(lv_now) = utclong_current( ).
" Add 1 day
DATA(lv_tomorrow) = utclong_add(
val = lv_now
days = 1
).
" Add 2 hours
DATA(lv_later) = utclong_add(
val = lv_now
hours = 2
).

utclong_diff() – Time Difference

DATA: lv_start TYPE utclong,
lv_end TYPE utclong.
" Difference in seconds
DATA(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 name
DATA(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

CategoryFunctions
Tableslines(), line_exists(), line_index()
Booleanxsdbool(), boolc()
String manipulationcondense(), to_upper(), to_lower(), to_mixed(), from_mixed()
String searchcontains(), matches(), find(), count()
String extractionsubstring(), substring_before(), substring_after(), segment()
String miscconcat_lines_of(), escape(), repeat(), reverse(), replace(), strlen()
Numericabs(), sign(), ceil(), floor(), trunc(), round(), nmin(), nmax(), ipow(), frac()
Date/Timeutclong_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() returns abap_true/abap_false – ideal for boolean expressions.
  • condense() replaces CONDENSE statement in expressions.
  • concat_lines_of() replaces complex LOOP constructs for string concatenation.
  • to_upper()/to_lower() are functions – TRANSLATE ... TO UPPER/LOWER is a statement.
  • Combine with String Templates for readable formatting.
  • nmin()/nmax() support up to 9 values.
  • matches() for regex checks, contains() for simple substring search.
  • Use CONV when type conversion is needed.