Count the number of occurences of a substring into a string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | string |
String. |
||
character(len=*), | intent(in) | :: | substring |
Substring. |
Number of occurrences.
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 endfunction count_substring