sindex_string_character Function

private elemental function sindex_string_character(self, substring, back) result(i)

Return the position of the start of the first occurrence of string substring as a substring in string, counting from one. If substring is not present in string, zero is returned. If the back argument is present and true, the return value is the start of the last occurrence rather than the first.

 type(string) :: string1
 logical      :: test_passed(2)
 string1 = 'Hello World Hello!'
 test_passed(1) = string1%index(substring='llo')==index(string='Hello World Hello!', substring='llo')
 test_passed(2) = string1%index(substring='llo', back=.true.)==index(string='Hello World Hello!', substring='llo', back=.true.)
 print '(L1)', all(test_passed)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self

The string.

character(kind=CK, len=*), intent(in) :: substring

Searched substring.

logical, intent(in), optional :: back

Start of the last occurrence rather than the first.

Return Value integer

Result of the search.


Called by

proc~~sindex_string_character~~CalledByGraph proc~sindex_string_character stringifor_string_t::string%sindex_string_character interface~index stringifor_string_t::index interface~index->proc~sindex_string_character

Contents


Source Code

   elemental function sindex_string_character(self, substring, back) result(i)
   !< Return the position of the start of the first occurrence of string `substring` as a substring in `string`, counting from one.
   !< If `substring` is not present in `string`, zero is returned. If the back argument is present and true, the return value is
   !< the start of the last occurrence rather than the first.
   !<
   !<```fortran
   !< type(string) :: string1
   !< logical      :: test_passed(2)
   !< string1 = 'Hello World Hello!'
   !< test_passed(1) = string1%index(substring='llo')==index(string='Hello World Hello!', substring='llo')
   !< test_passed(2) = string1%index(substring='llo', back=.true.)==index(string='Hello World Hello!', substring='llo', back=.true.)
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self      !< The string.
   character(kind=CK, len=*), intent(in)           :: substring !< Searched substring.
   logical,                   intent(in), optional :: back      !< Start of the last occurrence rather than the first.
   integer                                         :: i         !< Result of the search.

   if (allocated(self%raw)) then
      i = index(string=self%raw, substring=substring, back=back)
   else
      i = 0
   endif
   endfunction sindex_string_character