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)

2491

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)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP - Type Casting

Category - ABAP

How do you actually get the original type of a class or instance if it is passed in a generic table? In this article we examine the possibilities.

04/16/2024

ABAP - RETURN value

Category - ABAP

After all these years, the “real” return in ABAP has finally arrived. In this article we will show you how it works and what it can do.

02/13/2024

ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?

04/14/2023

ABAP Deep Dive - Table access (internal)

Category - ABAP

In this article, let's take a look at table access to internal tables and how they replace READ TABLE.

02/03/2023

ABAP Developer still relevant

Category - ABAP

In this article we look at whether ChatGPT can already replace an ABAP developer or whether it can be used as a help in everyday life.

01/06/2023