Previous Next

INDEX function
Returns an INTEGER value that indicates the position of the target string within the source string.
Syntax 
 
INDEX ( source , target [ , starting ] )
source
A CHARACTER or LONGCHAR expression.
target
A CHARACTER or LONGCHAR expression whose position you want to locate in source. If target does not exist within source, INDEX returns a 0.
starting
An integer that specifies at which left-most position in the string to start the search. For example, INDEX("abcdefabcdef","abc",6) returns 7.
Examples 
For this example, you must enter 1, 2, 3, 4, or 5. The INDEX function checks if the digit exists in the string "12345".
 
DEFINE VARIABLE x    AS CHARACTER NO-UNDO FORMAT "9"
  LABEL "Enter a digit between 1 and 5".
DEFINE VARIABLE show AS CHARACTER NO-UNDO FORMAT "x(5)" EXTENT 5 
  LABEL "Literal" INITIAL ["One", "Two", "Three", "Four", "Five"].
 
REPEAT:
  SET x AUTO-RETURN.
  IF INDEX("12345",x) = 0 THEN DO:
    MESSAGE "Digit must be 1,2,3,4, or 5. Try again.".
    UNDO, RETRY.
  END.
  ELSE DISPLAY show[INTEGER(x)].
END.
This procedure also uses the starting option:
 
DEFINE VARIABLE positions AS CHARACTER NO-UNDO FORMAT "x(60)".
DEFINE VARIABLE sentence  AS CHARACTER NO-UNDO FORMAT "x(72)".
DEFINE VARIABLE vowel     AS CHARACTER NO-UNDO FORMAT "x".
DEFINE VARIABLE found     AS INTEGER   NO-UNDO.
DEFINE VARIABLE loop      AS INTEGER   NO-UNDO.
DEFINE VARIABLE start     AS INTEGER   NO-UNDO.
 
FORM sentence LABEL "Type in a sentence"
  WITH FRAME top
  TITLE "This program will tell where the vowels are in a sentence.".
 
SET sentence WITH FRAME top.
DO loop = 1 TO 5:
  ASSIGN
    positions = ""
    vowel     = SUBSTRING("aeiou",loop,1)
    start     = 1
    found     = INDEX(sentence,vowel,start).
 
  DO WHILE found > 0:
    ASSIGN
      positions = positions + STRING(found) + " "
      start     = found + 1
      found     = INDEX(sentence,vowel,start).
  END.
  DISPLAY vowel LABEL "Vowel" positions LABEL "Is found at locations..." 
    WITH 5 DOWN.
  DOWN.
END.
Notes 
*
*
*
The INDEX function is double-byte enabled. You can specify target and source strings for the INDEX function that contain double-byte characters.
See also 
LOOKUP function, R-INDEX function

Previous Next
© 2013 Progress Software Corporation and/or its subsidiaries or affiliates.