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.

Note

The doctest is not necessary, this being tested by replace.

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 string%replace_one_occurrence proc~replace string%replace proc~replace->proc~replace_one_occurrence proc~unique string%unique proc~unique->proc~replace program~volatile_doctest~62 volatile_doctest program~volatile_doctest~62->proc~replace proc~split string%split proc~split->proc~unique program~volatile_doctest~41 volatile_doctest program~volatile_doctest~41->proc~unique proc~camelcase string%camelcase proc~camelcase->proc~split proc~glob_string string%glob_string proc~glob_string->proc~split proc~snakecase string%snakecase proc~snakecase->proc~split proc~split_chunked string%split_chunked proc~split_chunked->proc~split proc~startcase string%startcase proc~startcase->proc~split proc~write_lines~2 string%write_lines proc~write_lines~2->proc~split program~stringifor_test_csv_naive_parser stringifor_test_csv_naive_parser program~stringifor_test_csv_naive_parser->proc~split program~stringifor_test_parse_large_csv stringifor_test_parse_large_csv program~stringifor_test_parse_large_csv->proc~split program~stringifor_test_parse_large_csv->proc~split_chunked program~volatile_doctest~107 volatile_doctest program~volatile_doctest~107->proc~split program~volatile_doctest~15 volatile_doctest program~volatile_doctest~15->proc~split program~volatile_doctest~17 volatile_doctest program~volatile_doctest~17->proc~split program~volatile_doctest~23 volatile_doctest program~volatile_doctest~23->proc~split program~volatile_doctest~44 volatile_doctest program~volatile_doctest~44->proc~split proc~write_file~2 string%write_file program~volatile_doctest~44->proc~write_file~2 interface~glob glob interface~glob->proc~glob_string proc~glob_character string%glob_character interface~glob->proc~glob_character none~glob string%glob none~glob->proc~glob_string none~glob->proc~glob_character proc~write_file~2->proc~write_lines~2 program~volatile_doctest~18 volatile_doctest program~volatile_doctest~18->proc~camelcase program~volatile_doctest~2 volatile_doctest program~volatile_doctest~2->proc~startcase program~volatile_doctest~75 volatile_doctest program~volatile_doctest~75->proc~split_chunked program~volatile_doctest~98 volatile_doctest program~volatile_doctest~98->proc~snakecase proc~glob_character->none~glob program~volatile_doctest~100 volatile_doctest program~volatile_doctest~100->interface~glob program~volatile_doctest~103 volatile_doctest program~volatile_doctest~103->none~glob program~volatile_doctest~82 volatile_doctest program~volatile_doctest~82->none~glob

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