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.

Advertising

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)
Advertising

ABAP - Performance for the SELECT

Category - ABAP

In this article we will look at a few special cases with the SELECT and examine the performance of these constructs. We'll show you the current alternatives and give you little tips while reading.

04/02/2021

ABAP - Check objects (Instances)

Category - ABAP

In this article we will show you how you can analyze instances and react to them correctly, for example if you hand them over during processing and want to react individually.

03/19/2021

ABAP - Loops

Category - ABAP

With New ABAP, new loops and possibilities for restricting table contents were created. We'll take a closer look at these new commands in this article.

02/19/2021

ABAP - Comparison

Category - ABAP

Today we'll look at comparisons and comparison operators in terms of the new commands and their current usage. What has changed so far and what should still be done?

02/12/2021

ABAP - String Functions (Part 3)

Category - ABAP

This article is about other new string functions and a replacement for CONDENSE. We would like to show you why you should use the new functions.

01/22/2021

We use cookies for our services and functions. More informations