count_substring Function

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

Arguments

Type IntentOptional AttributesName
character(len=*), intent(in) :: string_

String.

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

Substring.

Return Value integer(kind=I4P)

Number of occurrences.

Description

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

Called By

proc~~count_substring~~CalledByGraph proc~count_substring count_substring interface~count count interface~count->proc~count_substring
Help

Variables

TypeVisibility AttributesNameInitial
integer(kind=I4P), public :: c1

Counters.

integer(kind=I4P), public :: c2

Counters.


Source Code

  elemental function count_substring(string_, substring) result(No)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Count the number of occurences of a substring into a string.
  !---------------------------------------------------------------------------------------------------------------------------------
  character(*), intent(in) :: string_   !< 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(string_)) return
  c1 = 1
  do
    c2 = index(string=string_(c1:), substring=substring)
    if (c2==0) return
    No = No + 1
    c1 = c1 + c2 + len(substring)
  enddo
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endfunction count_substring