Returns an INTEGER value that indicates the position of the target string within the source string. In contrast to the INDEX function, R-INDEX performs the search from right to left.
R-INDEX ( source , target , starting )A constant, field name, variable name, or expression that results in a CHARACTER or LONGCHAR value.A CHARACTER or LONGCHAR expression whose position you want to locate in source. If target does not exist within source, R-INDEX returns 0.If a starting parameter is not specified, then the search for the target pattern begins at the right-most character. Even though the search is started from the right, the target position is calculated from the left. For example, this code returns a 3 rather than a 2:
R-INDEX("abcd" , "c")An integer that specifies the begin point for the search. The search is right-to-left and starts from the starting point. For example, this statement returns 1 R-INDEX("abcdefabcdef","abc",6).This procedure prompts you to enter a character string and a pattern to match against the string. It then displays the starting position of the string where the pattern was found.
DEFINE VARIABLE rindx AS INTEGER NO-UNDO.DEFINE VARIABLE source AS CHARACTER NO-UNDO FORMAT "X(45)".DEFINE VARIABLE target AS CHARACTER NO-UNDO FORMAT "X(45)".REPEAT:PROMPT-FOR source LABEL "Enter a character string to do pattern matching:"WITH FRAME s1 CENTERED.PROMPT-FOR target LABEL "Enter a pattern to match in the string:"WITH FRAME t1 CENTERED.rindx = R-INDEX(INPUT source, INPUT target).IF rindx < > 0 THEN DO:DISPLAY "The target pattern:" INPUT target NO-LABEL"last appears in position" rindx NO-LABEL SKIPWITH FRAME r1 ROW 12 CENTERED.DISPLAY "in the source string:" INPUT source NO-LABELWITH FRAME r1 ROW 12 CENTERED.HIDE FRAME r1.END.IF rindx = 0 THEN DO:DISPLAY "The target pattern:" INPUT target NO-LABEL"could not be found" SKIPWITH FRAME r2 ROW 12 CENTERED.DISPLAY "in the source string:" INPUT source NO-LABELWITH FRAME r2 ROW 12 CENTERED.HIDE FRAME r2.END.END.This example also uses a starting value:
![]()
![]()
The R-INDEX function is double-byte enabled. You can specify target and source strings for the R-INDEX function that contain double-byte characters.
© 2012 Progress Software Corporation and/or its subsidiaries or affiliates. |