Type | Intent | Optional | 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. |
The string with old replaced by new.
Return a string with all occurrences of substring old replaced by new.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public | :: | r | Counter. |
elemental function replace(self, old, new, count) result(replaced)
!---------------------------------------------------------------------------------------------------------------------------------
!< Return a string with all occurrences of substring old replaced by new.
!---------------------------------------------------------------------------------------------------------------------------------
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
return
!---------------------------------------------------------------------------------------------------------------------------------
endfunction replace