Manual Reference Pages  - index (3m_unicode)

NAME

INDEX(3f) - [M_unicode:SEARCH] Position of a substring within a string (LICENSE:MIT)

CONTENTS

Synopsis
Characteristics
Description
Options
Result
Examples
See Also
Author
License

SYNOPSIS

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

CHARACTERISTICS

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.

DESCRIPTION

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.

OPTIONS

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.

RESULT

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.

EXAMPLES

Example program

   program demo_index
   use M_unicode, only : ut=>unicode_type
   use M_unicode, only : assignment(=)
   use M_unicode, only : index
   implicit none
   type(ut)                   :: str
   character(len=*),parameter :: all=’(*(g0))’
   integer                    :: ii
      !
      str=’Huli i kēia kaula no kēia ʻōlelo’
      !bug!print all, index(str,’kēia’).eq.8
      ii=index(str,’kēia’); print all, ii.eq.8
      !
      ! return value is counted from the left end even if BACK=.TRUE.
      !bug!print all, index(str,’kēia’,back=.true.).eq.22
      ii=index(str,’kēia’,back=.true.); print all, ii.eq.22
      !
      ! INDEX is case-sensitive
      !bug!print all, index(str,’Kēia’).eq.0
      ii=index(str,’Kēia’); print all, ii.eq.0
      !<<<<<<<<<<
      !ifx bug: ifx (IFX) 2024.1.0 20240308
      !
      !example/demo_index.f90(17): error #6766: A binary defined OPERATOR
      !definition is missing or incorrect.   [EQ]
      !        print all, index(str,’k  ia’,back=.true.).eq.22
      !--------------------------------------------------^
      !Original works with gfortran and flang_new and this works with ifx
      !        ii=ndex(str,’k  ia’,back=.true.)
      !    print all, ii.eq.22
      !>>>>>>>>>>
   end program demo_index

Expected Results:

   > T
   > T
   > T
   > T
   > T
   > T

SEE ALSO

Functions that perform operations on character strings, return lengths of arguments, and search for certain arguments:
o ELEMENTAL: ADJUSTL(3), ADJUSTR(3), INDEX(3), SCAN(3), VERIFY(3)
o NONELEMENTAL: LEN_TRIM(3), LEN(3), REPEAT(3), TRIM(3)

AUTHOR

John S. Urban

LICENSE

    MIT