Return a string with the first occurrence of substring old replaced by new.
Note
The doctest is not necessary, this being tested by replace.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(string), | intent(in) | :: | self |
The string. |
||
character(kind=CK, len=*), | intent(in) | :: | old |
Old substring. |
||
character(kind=CK, len=*), | intent(in) | :: | new |
New substring. |
The string with old replaced by new.
elemental function replace_one_occurrence(self, old, new) result(replaced) !< Return a string with the first occurrence of substring old replaced by new. !< !< @note The doctest is not necessary, this being tested by [[string:replace]]. class(string), intent(in) :: self !< The string. character(kind=CK, len=*), intent(in) :: old !< Old substring. character(kind=CK, len=*), intent(in) :: new !< New substring. type(string) :: replaced !< The string with old replaced by new. integer :: pos !< Position from which replace old. if (allocated(self%raw)) then replaced = self pos = index(string=self%raw, substring=old) if (pos>0) then if (pos==1) then replaced%raw = new//self%raw(len(old)+1:) else replaced%raw = self%raw(1:pos-1)//new//self%raw(pos+len(old):) endif endif endif endfunction replace_one_occurrence