private elemental function replace(self, old, new, count) result(replaced)
Return a string with all occurrences of substring old replaced by new.
type(string)::astringlogical::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)
Nodes of different colours represent the following:
Solid arrows point from a procedure to one which it calls. Dashed
arrows point from an interface to procedures which implement that interface.
This could include the module procedures in a generic interface or the
implementation in a submodule of an interface in a parent module.
Nodes of different colours represent the following:
Solid arrows point from a procedure to one which it calls. Dashed
arrows point from an interface to procedures which implement that interface.
This could include the module procedures in a generic interface or the
implementation in a submodule of an interface in a parent module.
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))thenreplaced=selfif(len(old)==0)return! avoid infite loop for null substring replacementr=0do if(index(replaced%raw,old)>0)thenreplaced=replaced%replace_one_occurrence(old=old,new=new)r=r+1if(present(count))then if(r>=count)exit endif else exit endif enddo endif endfunction replace