ABAP - String Functions (Part 3)
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.
Table of contents
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