replace_one_occurrence Function

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

Return a string with the first occurrence of substring old replaced by new.

Type Bound

string

Arguments

Type IntentOptional Attributes Name
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.

Return Value type(string)

The string with old replaced by new.


Called by

proc~~replace_one_occurrence~~CalledByGraph proc~replace_one_occurrence stringifor_string_t::string%replace_one_occurrence proc~replace stringifor_string_t::string%replace proc~replace->proc~replace_one_occurrence proc~unique stringifor_string_t::string%unique proc~unique->proc~replace proc~split stringifor_string_t::string%split proc~split->proc~unique proc~camelcase stringifor_string_t::string%camelcase proc~camelcase->proc~split proc~glob_string stringifor_string_t::string%glob_string proc~glob_string->proc~split proc~snakecase stringifor_string_t::string%snakecase proc~snakecase->proc~split proc~split_chunked stringifor_string_t::string%split_chunked proc~split_chunked->proc~split proc~startcase stringifor_string_t::string%startcase proc~startcase->proc~split proc~write_lines~2 stringifor_string_t::string%write_lines proc~write_lines~2->proc~split interface~glob stringifor_string_t::glob interface~glob->proc~glob_string proc~glob_character stringifor_string_t::string%glob_character interface~glob->proc~glob_character proc~write_file~2 stringifor_string_t::string%write_file proc~write_file~2->proc~write_lines~2 proc~glob_character->interface~glob program~volatile_doctest~16 volatile_doctest program~volatile_doctest~16->interface~glob program~volatile_doctest~69 volatile_doctest program~volatile_doctest~69->interface~glob program~volatile_doctest~80 volatile_doctest program~volatile_doctest~80->interface~glob

Contents


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