index

Date:

10-12-2011

NAME

INDEX - Determines the starting location of a character substring in a string

SYNOPSIS

INTEGER result, kind
CHARACTER string, substring
LOGICAL back

result = INDEX ([STRING=] string, [SUBSTRING=]substring [,[BACK=]back] [,[KIND=]kind])

STANDARDS

Fortran

DESCRIPTION

The INDEX intrinsic function returns the starting position of a substring within a string from either the right or left end of the string. It accepts the following arguments:

string

Must be of type character.

substring

Must be of type character.

back

Must be of type logical. If omitted, a value of false is assumed. If present with a value of true, the search begins at the end of string.

kind

Determines the kind type parameter of the return result. See the RETURN VALUES section for more information. This argument must be a scalar integer initialization expression.

INDEX() is an elemental function. The name of this intrinsic can be passed as an argument.

NOTES

When the INDEX() intrinsic function is passed as an actual argument, it must be called with only the first two arguments in the routine that calls the passed-in function.

RETURN VALUES

The result is of type INTEGER and its kind type parameter is dependent on the kind argument. If the kind argument is not passed, the kind type parameter of result is the same as that of the default integer type; otherwise it is of the kind type parameter specified by the kind argument.

The back argument can affect the return value as follows:

  • If back is absent or is present with the value FALSE, the INDEX search is relative to the left end of string. The result is the minimum positive value of i, as follows: string(i:i+ LEN(substring) - 1 ) =substring

  • If the length of substring is 0, a value of 1 is returned.

  • If back is present with the value TRUE, the INDEX() search is relative to the right end of string. The result is the maximum positive value of the following: i<= LEN(string) - LEN(substring) + 1

  • In the preceding equation, substring is as follows:string(i:i+ LEN(substring) - 1 ) =substring

  • If the length of substring is 0, the value returned is as follows: LEN(string) + 1

If substring occurs more than once in string, INDEX() returns the index to the first occurrence.

The return value is 0 if substring is not located within string.

The return value is 0 if the length of string is less than the length of substring.

EXAMPLES

Example 1: INDEX(‘FORTRAN’,’R’) yields 3.

Example 2: INDEX(‘FORTRAN’,’R’,BACK=.TRUE., KIND=4) yields 5.

Example 3: The following program returns the index I=6 for the substring SUPERCOMPUTER:

PROGRAM INDEX1
CHARACTER*22 A
CHARACTER*13 B
A = 'CRAY SUPERCOMPUTER'
B = 'SUPERCOMPUTER'
I = INDEX(A,B)
PRINT *, I
STOP
END