Scroll Top

New Features in ABAP 7.4 – String Processing

97be63abb66b42f281dff9197b6b35da_WglrP9Anthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 17 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at [email protected].

Working with Strings in ABAP 7.4

SAP has added some interesting new String Functions in the ABAP 7.2. In some cases the new functions replace previous ABAP commands, and in other cases they bring some completely new functionality. Lets take a closer look…

Here are some of the more important changes to string processing in ABAP 7.2 and ABAP 7.4:

» Chaining Operator: chain two character-like operands into one new character string.

» String Templates: the option to create a character string out of literal texts, expressions, and control characters.

» Character String Functions: built-in functions

Using the Chaining Operator in ABAP 7.4

The Chaining Operator && can be used to create one character string out of multiple other strings and literals. The use of the chaining operator largely replaces the CONCATENATE statement. In this example, three variables are concatenated together using the && chaining operator.

DATA: v_var1 TYPE char30,
      v_var2 TYPE char30,
      v_var3 TYPE char30.
DATA: lv_result TYPE string.

v_var1 = 'Building'.
v_var2 = 'A'.
v_var3 = 'String'.

lv_result = v_var1 && v_var2 && v_var3.
WRITE: /(30) 'Using &&', lv_result

Using String Templates in ABAP 7.4

A string template is defined by using the | (pipe) symbol at the beginning and end of a template.
DATA: character_string TYPE string.
character_string = |This is a literal text.|.
WRITE: /(30) character_string.

The added value of a string template becomes clear when combining literal texts with embedded expressions and control characters. Embedded expressions are defined within a string template with curly brackets { expression }. Note that a space between bracket and expression is obligatory. Some examples are:

character_string = |{ a_numeric_variable }|.
character_string = |This resulted in return code { sy-subrc }|.

Best of all, you can pass such constructs (the text between the starting | and the ending |) into parameters of method calls that are expecting strings. Previously, we had to create the string in a local variable and then use the local variable in the method call.

LO_OBJECT->STRING2XML( |{ converted_xml }{ xml_row-row_close_tag }| ).

Using Embedded Expressions in ABAP 7.2 and ABAP 7.4

In ABAP 7.2, the ALPHA formatting option was introduced and completely replaces the two function modules CONVERSION_EXIT_ALPHA_INPUT and CONVERSION_EXIT_ALPHA_OUTPUT. Now you can add or remove leading zeroes with this one .

Below is the syntax for the ALPHA Embedded Expressions.

ALPHA = IN|OUT|RAW|(val)]

The parameter IN can be used to transform numeric sequences without leading zeroes to the format of numeric text with leading zeroes. The parameter OUT can be used to convert numeric text with leading zeroes to numeric sequences without leading zeroes. RAW is no formatting, and the possibilities of (val) are defined as constants in the class CL_ABAP_FORMAT. Lets look at an example…

DATA: ld_message TYPE string.
DATA: ld_delivery_number TYPE vbeln_vl VALUE '0080003371'.
DATA: ls_delivery_header TYPE likp.

ld_message = |{ ld_delivery_number ALPHA = OUT }|.
WRITE: / ld_message.

BREAK-POINT.

SELECT *
  FROM likp
  INTO CORRESPONDING FIELDS OF ls_delivery_header
     WHERE vbeln = ld_delivery_number.
ENDSELECT.

If we run this in the debugger, we can see that we no longer have to add the leading zeroes back—as they were never actually removed from the delivery variable in the first place.

ABAP 7.4 ALPHA String Bult-In Func

There are more Embedded Expressions than ALPHA in ABAP 7.2 and ABAP 7.4. I would recommend checking out the ABAP 7.2 help, but here are a few more…

    [WIDTH     = len]
    [ALIGN     = LEFT|RIGHT|CENTER|(val)]
    [PAD       = c]
    [CASE      = RAW|UPPER|LOWER|(val)]
    [SIGN      = LEFT|LEFTPLUS|LEFTSPACE|RIGHT|RIGHTPLUS|RIGHTSPACE|(val)]
    [EXPONENT  = exp]
    [DECIMALS  = dec]
    [ZERO      = YES|NO|(val)]
    [XSD       = YES|NO|(val)]
    [STYLE     =  SIMPLE|SIGN_AS_POSTFIX|SCALE_PRESERVING
                 |SCIENTIFIC|SCIENTIFIC_WITH_LEADING_ZERO
                 |SCALE_PRESERVING_SCIENTIFIC|ENGINEERING
                 |(val)]
    [CURRENCY  = cur]
    [NUMBER    = RAW|USER|ENVIRONMENT|(val)]
    [DATE      = RAW|ISO|USER|ENVIRONMENT|(val)]
    [TIME      = RAW|ISO|USER|ENVIRONMENT|(val)]
    [TIMESTAMP = SPACE|ISO|USER|ENVIRONMENT|(val)]
    [TIMEZONE  = tz]
    [COUNTRY   = cty] ...

Summary

With the new string options like String Templates, Chaining of strings, and Embedded Expression formatting string functions, we have the ability to create ABAP code with fewer statements with the same functionality, without compromising readability of the code. If you’re working on a ABAP 7.2 – ABAP 7.4 based system, I would start using new ABAP coding techniques, this will improve your speed of development and make your programs easier to maintain.

ITP logo

If you enjoyed this blog, New Features in ABAP 7.4 – String Processing, please fill out the form below to sign up for our newsletter. We deliver SAP Technical tips & tricks, SAP news, and the current month’s BLOG right to your inbox!

Related Posts

Related Posts

Pin It on Pinterest

Share This

If you enjoyed this post, why not share it with your friends!