Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Expressions and Functions for String Processing →  String Functions →  Examples of String Functions 

String Functions, distance

The example demonstrates the string function distance.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: index_tab TYPE REF TO cl_abap_docu=>man_index_tab,
          langu     TYPE sy-langu,
          dist      TYPE i,
          max       TYPE i,
          str1      TYPE string,
          str2      TYPE string,
          BEGIN OF result,
            dist TYPE i,
            text TYPE string,
          END OF result,
          result_tab LIKE SORTED TABLE OF result
                     WITH NON-UNIQUE KEY dist,
          output TYPE string.
    FIELD-SYMBOLS: <index_tab> TYPE         cl_abap_docu=>man_index_tab,
                   <index>     TYPE LINE OF cl_abap_docu=>man_index_tab.
    IF sy-langu <> 'D'.
      langu = 'E'.
    ELSE.
      langu = 'D'.
    ENDIF.
    index_tab = cl_abap_docu=>get_abap_index( langu ).
    ASSIGN index_tab->* TO <index_tab>.
    LOOP AT <index_tab> ASSIGNING <index>.
      str1 = to_upper( val = <index>-key1 ).
      str2 = to_upper( val = word ).
      IF strlen( str1 ) > strlen( str2 ).
        max = strlen( str1 ).
      ELSE.
        max = strlen( str2 ).
      ENDIF.
      max = ( 100 - percent  ) * max / 100 + 1.
      dist = distance( val1 = str1 val2 = str2 max = max ).
      IF dist < max.
        result-dist = dist.
        result-text = str1.
        INSERT result INTO TABLE result_tab.
      ENDIF.
    ENDLOOP.
    LOOP AT result_tab INTO result.
      output = |{ result-text WIDTH = 40 }({ result-dist })|.
      WRITE: /  output.
    ENDLOOP.

Description

The program compares a text that has been input with the index entry in the ABAP keyword documentation using distance. The index entries that correspond to the longer texts to an arbitrary precentage are output in order of increasing edit distance.