This is a test message to test the length of the message box.
Login
ABAP String Functions (Part 3)
Created by Software-Heroes

ABAP - String Functions (Part 3)

12494

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.



We have already discussed the new and practical functions in the last parts of the string functions. Today we're going to show you a few new options, a replacement of old one-line commands and why you actually need all these new functions.

 

CONDENSE

The instruction is used to remove leading and trailing blanks in CHAR fields and strings. Furthermore, multiple spaces within the character string are compressed to one space. The instruction also offers a special function to remove all spaces.

In our examples we will refer to various constants and variables, so here are the basic definitions:

CONSTANTS:
  c_string TYPE string VALUE `  This is  my name  `,
  c_char   TYPE c LENGTH 25 VALUE '  This is  my name  '.    

DATA:
  ld_string TYPE string,
  ld_char   TYPE c LENGTH 25.

 

Hint: CHAR fields always fill the last digits with spaces, so the information above is actually superfluous. However, to keep the example unchanged, these have also been adopted.

 

Simple version

In the example, you will immediately notice that the new function works again with inline declaration and that we do not need our own variable for the transfer and we can work directly with the constant.

" Old
ld_string = c_string.
CONDENSE ld_string.

ld_char = c_char.
CONDENSE ld_char.    
    
" New
DATA(ld_new_string) = condense( c_string ).
DATA(ld_new_char) = condense( c_char ).

 

NO-GAPS

For the addition of the CONDENSE instruction, however, SAP does not provide any enhancement of the new function. But here you can fall back on a new function you have already learned about, the replace. Unfortunately, it shouldn't be written so briefly, but also very understandable in terms of implementation.

" Old
ld_string = c_string.
CONDENSE ld_string NO-GAPS.

ld_char = c_char.
CONDENSE ld_char NO-GAPS.    
    
" New
DATA(ld_new_string) = replace( val = c_string sub = ` ` with = `` occ = 0 ).
DATA(ld_new_char) = replace( val = c_char sub = ` ` with = `` occ = 0 ).

 

Substring

Also known as substring access, you only need part of a character or a string from time to time. Previously, the very short version of the offset and the length were used directly on the corresponding character string. The SUBSTRING function is now available for this, as in other programming languages.

" Old
ld_string = c_string+3(3).
ld_char = c_char+3(3).
    
" New
DATA(ld_new_string) = substring( val = c_string off = 3 len = 3 ).
DATA(ld_new_char) = substring( val = c_char off = 3 len = 3 ).

 

Usage

In the last few articles on these topics, you will have probably asked yourself why you should use the new functions at all. An advantage that we have already mentioned is of course the use and nesting within existing calls (chaining), as well as the possibility of inline declaration.

A crucial point, however, is the use in ABAP coding and the simultaneous modeling of Core Data Services (CDS). In a HANA environment, many actions with the data should be outsourced to the database via code pushdown. Therefore, all new commands can be found in this environment and only these can be used. It is therefore very useful to get used to the new functions.

 

Here is a brief explanation of the functions used to define the columns in the CDS. More information about string functions in SQL via the following link.

  • UPPER - Conversion of the field content to capital letters, deviation from the ABAP function TO_UPPER
  • CONCAT_WITH_SPACE - Merge the content with a space in between
  • REPLACE - Replace the semicolons in the city field with commas (e.g. for the CSV deduction)

 

Conclusion

As you have seen, not all new commands and functions are automatically shorter and easier to use. However, there are also advantages to switching directly to the new functions, especially if you are already working on a HANA system.

 

Source:
SAP Documentation - Condense
SAP Documentation - Replace
SAP Documentation - Substring


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 - XCO Libraries

Category - ABAP

What can you do with the library in ABAP and ABAP Cloud and how do you use the objects the best way? Find out more here.

11/12/2024

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