stretch(3f) - [M_strings:LENGTH] return string padded to at least
specified length
(LICENSE:PD)
function stretch(str,length,pattern,suffix) result(strout)
character(len=*),intent(in) :: str
integer,intent(in) :: length
character(len=*)intent(in),optional :: pattern
character(len=*)intent(in),optional :: suffix
character(len=:),allocatable :: strout
stretch(3f) pads a string with spaces to at least the specified
length. If the trimmed input string is longer than the requested
length the original string is returned trimmed of trailing spaces.
str the input string to return trimmed, but then padded to
the specified length if shorter than length
length The minimum string length to return
pattern optional string to use as padding. Defaults to a space.
suffix optional string to append to output string
strout The input string padded to the requested length or
the trimmed input string if the input string is
longer than the requested length.
Sample Program:
program demo_stretch use M_strings, only : stretch implicit none character(len=10) :: string=’abcdefghij’ character(len=:),allocatable :: answer integer :: i answer=stretch(string,5) write(,’(“[“,a,”]”)’) answer answer=stretch(string,20) write(,’(“[“,a,”]”)’) answer i=30 write(,) write(,’(1x,a,i0)’) & & stretch(‘CHAPTER 1 : The beginning ‘,i,’.’), 1 ,& & stretch(‘CHAPTER 2 : The end ‘,i,’.’), 1234 ,& & stretch(‘APPENDIX ‘,i,’.’), 1235 write(,) write(,’(1x,a,i7)’) & & stretch(‘CHAPTER 1 : The beginning ‘,i,’.’), 1 ,& & stretch(‘CHAPTER 2 : The end ‘,i,’.’), 1234 ,& & stretch(‘APPENDIX ‘,i,’.’), 1235 write(,) write(,) & & stretch(‘CHAPTER 1 : The beginning ‘,i,suffix=’: ‘), 1 write(,) & & stretch(‘CHAPTER 2 : The end ‘,i,suffix=’: ‘),1234 write(,) & & stretch(‘APPENDIX ‘,i,suffix=’: ‘), 1235 end program demo_stretch
Results:
[abcdefghij]
[abcdefghij ]
CHAPTER 1 : The beginning ....1
CHAPTER 2 : The end ..........1234
APPENDIX .....................1235
CHAPTER 1 : The beginning .... 1
CHAPTER 2 : The end .......... 1234
APPENDIX ..................... 1235
CHAPTER 1 : The beginning : 1
CHAPTER 2 : The end : 1234
APPENDIX : 1235
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | line | |||
integer, | intent(in) | :: | length | |||
character(len=*), | intent(in), | optional | :: | pattern | ||
character(len=*), | intent(in), | optional | :: | suffix |
function stretch(line,length,pattern,suffix) result(strout)
! ident_36="@(#) M_strings stretch(3f) return string padded to at least specified length"
character(len=*),intent(in) :: line
integer,intent(in) :: length
character(len=*),intent(in),optional :: pattern
character(len=*),intent(in),optional :: suffix
!-!character(len=max(length,len(trim(line)))) :: strout
character(len=:),allocatable :: strout
if(present(pattern))then
strout=pad(line,length,pattern)
else
strout=pad(line,length)
endif
if(present(suffix))then
strout=strout//suffix
endif
end function stretch