This is a test message to test the length of the message box.
Login
New ABAP String functions
Created by Software-Heroes

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.

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


Included topics:
New ABAPString functions
Comments (0)

ABAP Deep Dive - VALUE

Category - ABAP

In this article we want to look at the value statement again in all its forms and how you can use it in your daily work.

11/11/2022

ABAP - Common Table Expression (CTE)

Category - ABAP

In this article we want to take a look at the general table expression WITH and how you can use it in everyday life.

10/28/2022

ABAP Deep Dive - CORRESPONDING

Category - ABAP

In this article, a little more about the new Corresponding Statement and how to use it in detail. Let's take a look at the additional features.

09/16/2022

ABAP - Step

Category - ABAP

Today a new addition for the loops and how you can use it. With Step you have the possibility to skip steps in a loop.

09/02/2022

ABAP - Text, String, Template

Category - ABAP

How do I use character strings in modern ABAP development and how do they behave together with inline declarations. We also look at errors that can happen with it.

01/14/2022