replace_char Function

public pure function replace_char(str, s1, s2) result(newstr)

Replace all occurrences of a single character s1 in str with s2.

Arguments

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

original string

character(len=1), intent(in) :: s1

find all occurrences of this character

character(len=1), intent(in) :: s2

replace with this character

Return Value character(len=:), allocatable

new string


Source Code

    pure function replace_char(str, s1, s2) result(newstr)

    character(len=*),intent(in) :: str  !! original string
    character(len=1),intent(in) :: s1   !! find all occurrences of this character
    character(len=1),intent(in) :: s2   !! replace with this character
    character(len=:),allocatable :: newstr !! new string

    integer :: i !! counter

    newstr = str
    do i = 1, len(newstr)
        if (newstr(i:i) == s1) newstr(i:i) = s2
    end do

    end function replace_char