replace Function

private elemental function replace(self, old, new, count) 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
integer, intent(in), optional :: count

Return Value type(string)


Calls

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

Called by

proc~~replace~2~~CalledByGraph proc~replace~2 string%replace 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~2 string%write_lines proc~write_lines~2->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~2 string%write_file proc~write_file~2->proc~write_lines~2 proc~glob_character~2->none~glob~2

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