Fortran Library Reference

index,rindex,lnblnk: Index or Length of Substring

These functions search through a character string:

index(a1,a2)

Index of first occurrence of string a2 in string a1

rindex(a1,a2)

Index of last occurrence of string a2 in string a1

lnblnk(a1)

Index of last nonblank in string a1

index has the following forms:

index: First Occurrence of a Substring in a String

The index is an intrinsic function called by:

n = index( a1, a2 )

a1

character

Input 

Main string  

a2

character

Input 

Substring  

Return value 

INTEGER

Output 

n>0: Index of first occurrence of a2 in a1

n=0: a2 does not occur in a1.

If declared INTEGER*8, index() will return an INTEGER*8 value when compiled for a 64-bit environment and character variable a1 is a very large character string (greater than 2 Gigabytes).

rindex: Last Occurrence of a Substring in a String

The function is called by:

INTEGER*4 rindex

n = rindex( a1, a2 )

a1

character

Input 

Main string  

a2

character

Input 

Substring  

Return value 

INTEGER*4 orINTEGER*8

Output 

n>0: Index of last occurrence of a2 in a1

n=0: a2 does not occur in a1INTEGER*8 returned in 64-bit environments

lnblnk: Last Nonblank in a String

The function is called by:

n = lnblnk( a1 )

a1

character

Input 

String  

Return value 

INTEGER*4 orINTEGER*8

Output 

n>0: Index of last nonblank in a1

n=0: a1 is all nonblank INTEGER*8 returned in 64-bit environments

Example: index(), rindex(), lnblnk():


*                     123456789012345678901
    character s*24 / 'abcPDQxyz...abcPDQxyz' /
    INTEGER*4 declen, index, first, last, len, lnblnk, rindex
    declen = len( s )
    first = index( s, 'abc' )
    last = rindex( s, 'abc' )
    lastnb = lnblnk( s )
    write(*,*) declen, lastnb
    write(*,*) first, last
    end
demo% f77 -silent tindex.f 
demo% a.out 
24 21     <- declen is 24  because intrinsic len() returns the declared length of  s
1 13 


Note -

Programs compiled to run in a 64-bit environment must declare index, rindex and lnblnk (and their receiving variables) INTEGER*8 to handle very large character strings.