Replaces all the occurrences of substring ‘pattern’ in the input ‘string’ with the replacement ‘replacement’ Returns a new string
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | string | |||
| character(len=*), | intent(in) | :: | pattern | |||
| character(len=*), | intent(in) | :: | replacement |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private | :: | last | ||||
| integer, | private | :: | length_pattern | ||||
| integer, | private | :: | length_string | ||||
| integer, | private | :: | lps_array(len(pattern)) | ||||
| integer, | private | :: | p_i | ||||
| integer, | private | :: | s_i |
pure function replace_all_char_char_char(string, pattern, replacement) result(res) character(len=*), intent(in) :: string character(len=*), intent(in) :: pattern character(len=*), intent(in) :: replacement character(len=:), allocatable :: res integer :: lps_array(len(pattern)) integer :: s_i, p_i, last, length_string, length_pattern res = "" length_string = len(string) length_pattern = len(pattern) last = 1 if (length_pattern > 0 .and. length_pattern <= length_string) then lps_array = compute_lps(pattern) s_i = 1 p_i = 1 do while (s_i <= length_string) if (string(s_i:s_i) == pattern(p_i:p_i)) then if (p_i == length_pattern) then res = res// & & string(last:s_i - length_pattern)// & & replacement last = s_i + 1 p_i = 0 end if s_i = s_i + 1 p_i = p_i + 1 else if (p_i > 1) then p_i = lps_array(p_i - 1) + 1 else s_i = s_i + 1 end if end do end if res = res//string(last:length_string) end function replace_all_char_char_char