ABAP - String functions (Part 2)
There are many new inline functions in New ABAP. We want to deal with those for string processing today and show you some advantages.
Table of contents
With the many new inline functions that were introduced and also with the change in the processing and construction of strings, many functions for the string processing have been renewed again. A call as a concatenated statement or directly in a query is now possible.
In today's article we want to go over a few of these features and show you the benefits and innovations that come with them.
to_upper/to_lower
As the new commands clearly state, this is a case-sensitive conversion. For example, you can convert the value for a litter without changing the contents of the variable. The comparison in the example results in the output of 'Found!'.
" Old
DATA: ld_value TYPE string VALUE 'TESTTEXT'.
TRANSLATE ld_value TO UPPER CASE.
" New
DATA: ld_compare TYPE string VALUE 'TESTTEXT'.
IF to_upper( |Testtext| ) = ld_compare.
WRITE: / 'Found!'.
ENDIF.
replace
Finds and replaces strings within a string and returns the new value as a string. In the normal version, only one occurrence of the substring is replaced, for which the parameter "occ" must be set.
" Old
DATA(ld_text) = 'my pony is my pony and this is my house'.
REPLACE ALL OCCURRENCES OF 'my' IN ld_text WITH 'your'.
" New
DATA(ld_new) = replace(
val = 'my pony is my pony and this is my house'
sub = 'my'
with = 'your'
occ = 0
).
substring
The substring function extracts a substring from a string and returns it. The function can also be linked in combination with an inline declaration or used in an if-statement.
" Old
DATA ld_text TYPE string VALUE 'My ABAP course'.
ld_text = ld_text 3(4).
" New
DATA(ld_part) = substring( val = 'My ABAP course' off = 3 len = 4 ).
concat_lines_of
The command joins the contents of the table rows and returns a new string of full length. In our example, the old method still has the disadvantage that finally a space is added. With a few more lines of code, this fact can also be corrected.
" Preparation
DATA:
lt_sstr TYPE string_table.
ld_text TYPE string.
APPEND `This` TO lt_sstr.
APPEND `is` TO lt_sstr.
APPEND `my` TO lt_sstr.
APPEND `new` TO lt_sstr.
APPEND `house` TO lt_sstr.
" Old
LOOP AT lt_sstr INTO DATA(ld_sub).
ld_text = ld_text && ld_sub && ` `.
ENDLOOP.
" New
ld_text = concat_lines_of( table = lt_sstr sep = ` ` ).
Conclusion
Many of the new inline features already exist in an old way. The new features have the advantage that they can be linked together, used in if-statements and generate a new value. This makes them much more flexible than the old functions.
Source:
cadaxo - New string functions
SAP Documentation overview