str_replace Function

public function str_replace(string, old_str, new_str, place)

Function that replaces a string with another string

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: string

string to be modified

character(len=*), intent(in) :: old_str
character(len=*), intent(in) :: new_str
integer(kind=I4), intent(in) :: place

Return Value character(len=:), allocatable

returned string


Called by

proc~~str_replace~~CalledByGraph proc~str_replace str_replace program~test_files test_files program~test_files->proc~str_replace

Source Code

   function str_replace(string, old_str, new_str , place)
   !! Function that replaces a string with another string
   implicit none
   character(len = :), allocatable :: str_replace  !! returned string
   character(len = *), intent(in) :: string        !! string to be modified
   character(len = *), intent(in) :: old_str
   character(len = *), intent(in) :: new_str
   integer(kind = I4), intent(in) :: place

      integer(kind = I4) :: ind

      select case ( place )
         case (0, 2)
            ind = index( string, old_str )
         case (1)
            ind = index( string, old_str, back = .true. )
         case default
            stop 'str_replace bad choice'
      endselect

      str_replace = string(1:ind - 1) // new_str // string(ind + len(old_str):len(string))

      if ( place == 2) then
         do
            ind = index( str_replace, old_str )
            if ( ind == 0 ) exit
            str_replace = str_replace(1:ind - 1) // new_str // str_replace(ind + len(old_str):len(str_replace))
         enddo
      endif

   return
   endfunction str_replace