Processing Character Strings

Objective

After completing this lesson, you will be able to Process character strings in an application program.

Character String Functions

Process Character Strings

Processing Strings

The pipe symbol can be used to define a string template

String templates are ABAP expressions of result type string. You can use string templates in any reading operand position, for example, the right-hand side of a value assignment.

A string template begins and ends with a pipe symbol ( | | ). The simplest possible string template contains nothing but literal text. In this form, a string template is not really different from a string literal.

What distinguishes a string template from a string literal is the ability to embed expressions. An embedded expression is an ABAP expression surrounded by a pair of curly brackets ( { } ). At runtime, ABAP evaluates the embedded expression and translates the result into a string. In the result, this string replaces the embedded expression (together with the surrounding curly brackets).

Note

ABAP syntax requires at least one blank after the opening bracket and at least one blank before the closing bracket.

One string template can contain more than one embedded expression.

Inside the curly brackets you can place any kind of ABAP expression: variables, literals, or arithmetic expressions.

Examples of using string templates

Joining String

The operator && can be used to concatenate strings

You can join fields together using the concatenation operator &&. You can join any combination of data objects and string expressions.

The variables above (Part1 and Part2) of the expression are joined with no space or other separator between them. If you need spaces or another separator or character, you must remember to insert it yourself as part of the expression, as shown in the figure above.

Using the string example, users may wish to split their full name so that you can output just the first name.

The keyword SPLIT can be used to split a string at a given character into multiple substrings

Process Character Strings

Business Example

You have the full name of a customer but you are required to display the first name. Create the ABAP code to split the full name into first name and last name and output the first name to the console.

Note

In this exercise XX refers to your number.

Steps

  1. Create class ZCL_S4D100_XX_COND to ZCL_S4D100_XX_CASE.

    1. Expand CLASS ZLCOAL in the Project Explorer.

    2. Right-click on package ZS4D100_XX and choose NewABAP Class.

    3. In Name enter ZCL_S4D100_XX_CHAR.

    4. In Description: enter Character String.

    5. Choose Add to add the interface IF_OO_ADT_CLASSRUN.

    6. Choose OK.

    7. Choose Next.

    8. Choose your transport request and press Finish.

    9. Press Activate (Ctrl+F3).

  2. Change the code in if_oo_adt_classrun~main to use a CASE statement rather than an IF statement to check if c_number0 is initial or not.

    1. Implement the METHOD IF_OO_ADT_CLASSRUN~MAIN. with the following code:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      DATA: lv_full_name TYPE string VALUE 'Stan Wilson', lv_first_name TYPE string, lv_last_name TYPE string. SPLIT lv_full_name AT '' INTO lv_first_name lv_last_name. out->write( |user { lv_first_name }| ).
  3. Activate and test the program.

    1. Press Activate (Ctrl+F3).

    2. Press F9 to run the class.

    Practice

Log in to track your progress & complete quizzes