INDEX

Elemental Intrinsic Function (Generic): Returns the starting position of a substring within a string.

Syntax

result = INDEX (string, substring [, back] [, kind])

string
(Input) Must be of type character.

substring
(Input) Must be of type character.

back
(Optional; input) Must be of type logical.

kind
(Optional; input) Must be a scalar integer initialization expression.

Results:

The result is of type integer. If kind is present, the kind parameter of the result is that specified by kind; otherwise, the kind parameter of the result is that of default integer. If the processor cannot represent the result value in the kind of the result, the result is undefined.

If back does not appear (or appears with the value false), the value returned is the minimum value of I such that string (I : I + LEN (substring) - 1) = substring (or zero if there is no such value). If LEN (string) < LEN (substring), zero is returned. If LEN (substring) = zero, 1 is returned.

If back appears with the value true, the value returned is the maximum value of I such that string (I : I + LEN (substring) - 1) = substring (or zero if there is no such value). If LEN(string) < LEN (substring), zero is returned. If LEN (substring) = zero, LEN (string) + 1 is returned.

Specific Name Argument Type Result Type
INDEX CHARACTER INTEGER(4)
  CHARACTER INTEGER(8)

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: SCAN

Examples

INDEX ('FORTRAN', 'O', BACK = .TRUE.) has the value 2.

INDEX ('XXXX', " ", BACK = .TRUE.) has the value 5.

The following shows another example:

  I = INDEX('banana','an', BACK = .TRUE.) ! returns 4
  I = INDEX('banana', 'an') ! returns 2