Replace substring (only first occurrence) into a string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | string |
String to be modified. |
||
character(len=*), | intent(in) | :: | substring |
Substring to be replaced. |
||
character(len=*), | intent(in) | :: | restring |
String to be inserted. |
New modified string.
pure function replace(string, substring, restring) result(newstring) !< Replace substring (only first occurrence) into a string. character(len=*), intent(in) :: string !< String to be modified. character(len=*), intent(in) :: substring !< Substring to be replaced. character(len=*), intent(in) :: restring !< String to be inserted. character(len=:), allocatable :: newstring !< New modified string. integer(I4P) :: pos !< Position from which replace the substring. pos = index(string=string, substring=substring) newstring = string if (pos>0) then if (pos==1) then newstring = restring//string(len(substring)+1:) else newstring = string(1:pos-1)//restring//string(pos+len(substring):) endif endif endfunction replace