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 = 'llo'
test_passed(1) = index(s='Hello World Hello!', substring=string1)==index(string='Hello World Hello!', substring='llo')
test_passed(2) = index(s='Hello World Hello!', substring=string1, back=.true.)==index(string='Hello World Hello!', &
substring='llo', back=.true.)
print '(L1)', all(test_passed)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=CK, len=*), | intent(in) | :: | s |
String. |
||
type(string), | intent(in) | :: | substring |
Searched substring. |
||
logical, | intent(in), | optional | :: | back |
Start of the last occurrence rather than the first. |
Result of the search.
elemental function sindex_character_string(s, 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 = 'llo' !< test_passed(1) = index(s='Hello World Hello!', substring=string1)==index(string='Hello World Hello!', substring='llo') !< test_passed(2) = index(s='Hello World Hello!', substring=string1, back=.true.)==index(string='Hello World Hello!', & !< substring='llo', back=.true.) !< print '(L1)', all(test_passed) !<``` !=> T <<< character(kind=CK, len=*), intent(in) :: s !< String. type(string), 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(substring%raw)) then i = index(string=s, substring=substring%raw, back=back) else i = 0 endif endfunction sindex_character_string