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(3)
 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))
 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 stringifor_string_t::string%replace proc~replace_one_occurrence stringifor_string_t::string%replace_one_occurrence proc~replace->proc~replace_one_occurrence

Called by

proc~~replace~~CalledByGraph proc~replace stringifor_string_t::string%replace 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


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(3)
   !< 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))
   !< 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
      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