replace_one_occurrence Function

private elemental function replace_one_occurrence(self, old, new) result(replaced)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self
character(kind=CK, len=*), intent(in) :: old
character(kind=CK, len=*), intent(in) :: new

Return Value type(string)


Called by

proc~~replace_one_occurrence~2~~CalledByGraph proc~replace_one_occurrence~2 string%replace_one_occurrence proc~replace~2 string%replace proc~replace~2->proc~replace_one_occurrence~2 proc~unique~2 string%unique proc~unique~2->proc~replace~2 proc~split~2 string%split proc~split~2->proc~unique~2 proc~camelcase~2 string%camelcase proc~camelcase~2->proc~split~2 proc~glob_string~2 string%glob_string proc~glob_string~2->proc~split~2 proc~snakecase~2 string%snakecase proc~snakecase~2->proc~split~2 proc~split_chunked~2 string%split_chunked proc~split_chunked~2->proc~split~2 proc~startcase~2 string%startcase proc~startcase~2->proc~split~2 proc~write_lines~4 string%write_lines proc~write_lines~4->proc~split~2 interface~glob~2 glob interface~glob~2->proc~glob_string~2 proc~glob_character~2 string%glob_character interface~glob~2->proc~glob_character~2 none~glob~2 string%glob none~glob~2->proc~glob_string~2 none~glob~2->proc~glob_character~2 proc~write_file~4 string%write_file proc~write_file~4->proc~write_lines~4 proc~glob_character~2->none~glob~2

Source Code

   elemental function replace_one_occurrence(self, old, new) result(replaced)
   !< Return a string with the first occurrence of substring old replaced by new.
   !<
   !< @note The doctest is not necessary, this being tested by [[string:replace]].
   class(string),             intent(in)  :: self      !< The string.
   character(kind=CK, len=*), intent(in)  :: old       !< Old substring.
   character(kind=CK, len=*), intent(in)  :: new       !< New substring.
   type(string)                           :: replaced  !< The string with old replaced by new.
   integer                                :: pos       !< Position from which replace old.

   if (allocated(self%raw)) then
      replaced = self
      pos = index(string=self%raw, substring=old)
      if (pos>0) then
         if (pos==1) then
            replaced%raw = new//self%raw(len(old)+1:)
         else
            replaced%raw = self%raw(1:pos-1)//new//self%raw(pos+len(old):)
         endif
      endif
   endif
   endfunction replace_one_occurrence