INDEX(3) - [CHARACTER:SEARCH] Position of a substring within a string
result = index( string, substring [,back] [,kind] )
elemental integer(kind=KIND) function index(string,substring,back,kind)character(len=*,kind=KIND),intent(in) :: string character(len=*,kind=KIND),intent(in) :: substring logical(kind=**),intent(in),optional :: back integer(kind=**),intent(in),optional :: kind
o STRING is a character variable of any kind o SUBSTRING is a character variable of the same kind as STRING o BACK is a logical variable of any supported kind o KIND is a scalar integer constant expression.
INDEX(3) returns the position of the start of the leftmost or rightmost occurrence of string SUBSTRING in STRING, counting from one. If SUBSTRING is not present in STRING, zero is returned.
o STRING : string to be searched for a match o SUBSTRING : string to attempt to locate in STRING o BACK : If the BACK argument is present and true, the return value is the start of the rightmost occurrence rather than the leftmost. o KIND : if KIND is present, the kind type parameter is that specified by the value of KIND; otherwise the kind type parameter is that of default integer type.
The result is the starting position of the first substring SUBSTRING found in STRING.
If the length of SUBSTRING is longer than STRING the result is zero.
If the substring is not found the result is zero.
If BACK is .true. the greatest starting position is returned (that is, the position of the right-most match). Otherwise, the smallest position starting a match (ie. the left-most match) is returned.
The position returned is measured from the left with the first character of STRING being position one.
Otherwise, if no match is found zero is returned.
Example program
program demo_index implicit none character(len=*),parameter :: str=& Search this string for this expression !1234567890123456789012345678901234567890 write(*,*)& index(str,this).eq.8, & ! return value is counted from the left end even if BACK=.TRUE. index(str,this,back=.true.).eq.24, & ! INDEX is case-sensitive index(str,This).eq.0 end program demo_indexExpected Results:
> T T T
FORTRAN 77 , with KIND argument Fortran 2003
Functions that perform operations on character strings, return lengths of arguments, and search for certain arguments:
Fortran intrinsic descriptions
o ELEMENTAL: ADJUSTL(3), ADJUSTR(3), INDEX(3), SCAN(3), VERIFY(3) o NONELEMENTAL: LEN_TRIM(3), LEN(3), REPEAT(3), TRIM(3)
Nemo Release 3.1 | index (3fortran) | November 02, 2024 |