SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
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.
Source Code
REPORT demo_string_distance.
PARAMETERS: word TYPE c LENGTH 30 DEFAULT 'CALL METHOD' OBLIGATORY,
percent TYPE i DEFAULT 50 OBLIGATORY.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA: index_tab TYPE REF TO cl_abap_docu=>abap_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=>abap_index_tab,
<index> TYPE LINE OF cl_abap_docu=>abap_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.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
AT SELECTION-SCREEN ON percent.
IF percent NOT BETWEEN 0 AND 100.
MESSAGE 'Enter a value between 0 and 100' TYPE 'E'.
ENDIF.
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.