Replace all occurrences of a single character s1 in str with s2.
| Type | Intent | Optional | 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 |
new string
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