replace Function

private elemental function replace(self, old, new, count) result(replaced)

Return a string with all occurrences of substring old replaced by new.

 type(string) :: astring
 logical      :: test_passed(4)
 astring = 'When YOU are sad YOU should think to me :-)'
 test_passed(1) = (astring%replace(old='YOU', new='THEY')//''=='When THEY are sad THEY should think to me :-)')
 test_passed(2) = (astring%replace(old='YOU', new='THEY', count=1)//''=='When THEY are sad YOU should think to me :-)')
 astring = repeat(new_line('a')//'abcd', 20)
 astring = astring%replace(old=new_line('a'), new='|cr|')
 astring = astring%replace(old='|cr|', new=new_line('a')//'    ')
 test_passed(3) = (astring//''==repeat(new_line('a')//'    '//'abcd', 20))
 astring = 'abcd  efg    hlmn'
 astring = astring%replace(old='', new='-')
 test_passed(4) = (astring//''=='abcd  efg    hlmn')
 print '(L1)', all(test_passed)

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.

integer, intent(in), optional :: count

Number of old occurences to be replaced.

Return Value type(string)

The string with old replaced by new.


Calls

proc~~replace~~CallsGraph proc~replace string%replace proc~replace_one_occurrence string%replace_one_occurrence proc~replace->proc~replace_one_occurrence

Called by

proc~~replace~~CalledByGraph proc~replace string%replace 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(self, old, new, count) result(replaced)
   !< Return a string with all occurrences of substring old replaced by new.
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(4)
   !< astring = 'When YOU are sad YOU should think to me :-)'
   !< test_passed(1) = (astring%replace(old='YOU', new='THEY')//''=='When THEY are sad THEY should think to me :-)')
   !< test_passed(2) = (astring%replace(old='YOU', new='THEY', count=1)//''=='When THEY are sad YOU should think to me :-)')
   !< astring = repeat(new_line('a')//'abcd', 20)
   !< astring = astring%replace(old=new_line('a'), new='|cr|')
   !< astring = astring%replace(old='|cr|', new=new_line('a')//'    ')
   !< test_passed(3) = (astring//''==repeat(new_line('a')//'    '//'abcd', 20))
   !< astring = 'abcd  efg    hlmn'
   !< astring = astring%replace(old='', new='-')
   !< test_passed(4) = (astring//''=='abcd  efg    hlmn')
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   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.
   integer,                   intent(in), optional :: count    !< Number of old occurences to be replaced.
   type(string)                                    :: replaced !< The string with old replaced by new.
   integer                                         :: r        !< Counter.

   if (allocated(self%raw)) then
      replaced = self
      if (len(old)==0) return ! avoid infite loop for null substring replacement
      r = 0
      do
         if (index(replaced%raw, old)>0) then
            replaced = replaced%replace_one_occurrence(old=old, new=new)
            r = r + 1
            if (present(count)) then
               if (r>=count) exit
            endif
         else
            exit
         endif
      enddo
   endif
   endfunction replace