Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(string), | intent(in) | :: | self | The string. |
||
character(len=*), | intent(in) | :: | substring | Substring. |
||
integer, | intent(in) | :: | pos | Position from which insert substring. |
Inserted string.
Insert substring into string at a specified position.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public | :: | safepos | Safe position from which insert substring. |
elemental function insert_character(self, substring, pos) result(inserted)
!---------------------------------------------------------------------------------------------------------------------------------
!< Insert substring into string at a specified position.
!---------------------------------------------------------------------------------------------------------------------------------
class(string), intent(in) :: self !< The string.
character(len=*), intent(in) :: substring !< Substring.
integer, intent(in) :: pos !< Position from which insert substring.
type(string) :: inserted !< Inserted string.
integer :: safepos !< Safe position from which insert substring.
!---------------------------------------------------------------------------------------------------------------------------------
!---------------------------------------------------------------------------------------------------------------------------------
if (allocated(self%raw)) then
inserted = self
safepos = min(max(1, pos), len(self%raw))
if (safepos==1) then
inserted%raw = substring//self%raw
elseif (safepos==len(self%raw)) then
inserted%raw = self%raw//substring
else
inserted%raw = self%raw(1:safepos-1)//substring//self%raw(safepos:)
endif
else
inserted%raw = substring
endif
return
!---------------------------------------------------------------------------------------------------------------------------------
endfunction insert_character