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

Implement a CASE Statement

Business Example

Using the CASE ... ENDCASE keywords, users may want to create and evaluate the logical conditions in an ABAP program and output the results. Create a new application program and use the keywords CASE ... ENDCASE and output the result.

Note

In this exercise XX refers to your number.

Steps

  1. Copy your class ZCL_S4D100_XX_COND to ZCL_S4D100_XX_CASE.

    1. Right-click on CLASS ZCL_S4D100_XX_COND in the Project Explorer and choose Duplicate.

    2. Enter your package and name and new class name ZCL_S4D100_XX_CASE and press Next.

    3. Add it to your transport request and press Finish.

  2. Activate the application.

    1. Press Activate (Ctrl+F3).

  3. 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
      123456789101112
      CONSTANTS: c_number0 TYPE i VALUE 0. out->write( '--------------------------' ). out->write( 'Example 2: CASE...ENDCASE' ). out->write( '--------------------------' ). CASE c_number0. WHEN 0. out->write( 'The value of c_number0 equels zero' ). WHEN 1. out->write( 'The value of c_number0 equels some numer other than zero' ). ENDCASE.
  4. Activate and test the program.

    1. Press Activate (Ctrl+F3).

    2. Press F9 to run the class.

    Practice