ReplaceStr Function

private pure recursive function ReplaceStr(string, search, substitute) result(modifiedString)

Arguments

TypeIntentOptionalAttributesName
character, intent(in) :: string
character, intent(in) :: search
character, intent(in) :: substitute

Return Value character,allocatable


Contents

Source Code


Source Code

  pure recursive function ReplaceStr(string,search,substitute) result(modifiedString)
    implicit none
    character(*),intent(in)  :: string,search,substitute
    character(:),allocatable :: modifiedString
    integer                       :: i,stringLen,searchLen
    stringLen = len(string)
    searchLen = len(search)
    if(stringLen == 0 .or. searchLen == 0) then
      modifiedString = ''
      return
    elseif(stringLen < searchLen) then
      modifiedString = string
      return
    endif
    i = 1
    do
      if(string(i:i+searchLen-1) == search) then
        modifiedString = string(1:i-1)//substitute//replaceStr(string(i+searchLen:stringLen),search,substitute)
        exit
      endif
      if(i+searchLen > stringLen) then
        modifiedString = string
        exit
      endif
      i = i+1
      cycle
    enddo
  endfunction ReplaceStr