Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

INDEX

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

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

string

(Input) Must be of type character.

substring

(Input) Must be of type character.

back

(Input; optional) Must be of type logical.

kind

(Input; optional) Must be a scalar integer constant 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

CHARACTER

INTEGER(1)

CHARACTER

INTEGER(2)

INDEX 1

CHARACTER

INTEGER(4)

CHARACTER

INTEGER(8)

1The setting of compiler options specifying integer size can affect this function.

Example

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

See Also