count_substring Function

private elemental function count_substring(s, substring) result(No)

Count the number of occurences of a substring into a string.

 print "(L1)", count('hello', substring='ll')==1

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: s

String.

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

Substring.

Return Value integer(kind=I4P)

Number of occurrences.


Called by

proc~~count_substring~~CalledByGraph proc~count_substring stringifor_string_t::count_substring interface~count stringifor_string_t::count interface~count->proc~count_substring

Contents

Source Code


Source Code

   elemental function count_substring(s, substring) result(No)
   !< Count the number of occurences of a substring into a string.
   !<
   !<```fortran
   !< print "(L1)", count('hello', substring='ll')==1
   !<```
   !=> T <<<
   character(*), intent(in) :: s         !< String.
   character(*), intent(in) :: substring !< Substring.
   integer(I4P)             :: No        !< Number of occurrences.
   integer(I4P)             :: c1        !< Counters.
   integer(I4P)             :: c2        !< Counters.

   No = 0
   if (len(substring) > len(s)) return
   c1 = 1
   do
     c2 = index(string=s(c1:), substring=substring)
     if (c2==0) return
     No = No + 1
     c1 = c1 + c2 + len(substring)
   enddo
   endfunction count_substring