Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Expressions and Functions for Conditions →  log_exp - Logical Expressions →  rel_exp - Comparison Expressions →  rel_exp - Comparison Rules →  rel_exp - Comparing Elementary Data Types →  rel_exp - Comparison Type of Elementary Data Objects →  rel_exp - Comparison Type of Character-Like Data Objects 

Comparing Text Strings of Different Length

This example demonstrates how text strings of different lengths are compared.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA length1 TYPE n LENGTH 1 VALUE '4'.
    cl_demo_input=>add_field( CHANGING field = length1 ).
    DATA length2 TYPE n LENGTH 1 VALUE '6'.
    cl_demo_input=>request( CHANGING field = length2 ).

    DATA(len1) = CONV i( length1 ).
    DATA(len2) = CONV i( length2 ).
    IF len1 = 0 OR len2 = 0.
      cl_demo_output=>display( 'Try again!' ).
      RETURN.
    ENDIF.

    DATA(text1) = REDUCE string( INIT str = ``
                                FOR i = 0 UNTIL i >= len1
                                NEXT str = str && `X`  ).
    DATA(text2) = REDUCE string( INIT str = ``
                                FOR i = 0 UNTIL i >= len2
                                NEXT str = str && `X`  ).

    cl_demo_output=>display(
      COND #( WHEN text1 < text2 THEN |{ text1 } < { text2 }|
              WHEN text1 > text2 THEN |{ text1 } > { text2 }|
                                ELSE |{ text1 } = { text2 }| ) ).

Description

Two text strings text1 and text2 are filled using their respective input length with the character X and then compared. The shorter text string is smaller than the longer one.