Try OpenEdge Now
skip to main content
ABL Reference
ABL Syntax Reference : R-INDEX function
 

R-INDEX function

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.

Syntax

R-INDEX ( source , target [ , starting ] )
source
A constant, field name, variable name, or expression that results in a CHARACTER or LONGCHAR value.
target
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")
starting
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).

Examples

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.
r-rindex.p
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 SKIP
      WITH FRAME r1 ROW 12 CENTERED.
DISPLAY "in the source string:" INPUT source NO-LABEL
      WITH 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" SKIP
WITH FRAME r2 ROW 12 CENTERED.
DISPLAY "in the source string:" INPUT source NO-LABEL
WITH FRAME r2 ROW 12 CENTERED.
HIDE FRAME r2.
END.
END.
This example also uses a starting value:
r-rndex.p
DEFINE VARIABLE mark       AS INTEGER   NO-UNDO.
DEFINE VARIABLE line-width AS INTEGER   NO-UNDO.
DEFINE VARIABLE paragraph  AS CHARACTER NO-UNDO.

paragraph = "The course centers around an existing small "
+ "application that you modify to improve perfo"
+ "rmance. Our highly-qualified instructors dem"
+ "onstrate proven analysis and coding techniqu"
+ "es and provide tips for making the most of y"
+ "our ABL code. You are encouraged to bri"
+ "ng your own application problems to class an"
+ "d actively participate in class discussions "
+ "and hands-on lab exercises.".

SET line-width LABEL "Justify with how many characters wide?"
  VALIDATE(line-width >= 20 AND line-width <= 70,
    "Must be between 20 and 70 for this example.")
  WITH SIDE-LABELS FRAME ask.

FORM paragraph FORMAT "x(72)"
  WITH DOWN NO-LABELS USE-TEXT.

DISPLAY "L" + FILL("-", line-width - 2) + "R" @ paragraph.
DOWN.

DO WHILE LENGTH(paragraph) > line-width:
  mark = R-INDEX(paragraph, " ", line-width).
  DISPLAY SUBSTRING(paragraph, 1, mark) @ paragraph.
  DOWN.
  paragraph = SUBSTRING(paragraph, mark + 1).
END.

IF paragraph <> "" THEN
  DISPLAY paragraph.

Notes

*If either operand is case sensitive, then the R-INDEX function is also case sensitive.
*If either the source string or target pattern is null, the result is 0.
*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.

See also

INDEX function, LOOKUP function