replace Function

private elemental function replace(self, old, new, count) result(replaced)

Arguments

Type IntentOptional AttributesName
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.

Description

Return a string with all occurrences of substring old replaced by new.


Variables

TypeVisibility AttributesNameInitial
integer, public :: r

Counter.


Source Code

  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