Type | Intent | Optional | 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 |
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