pad(3f) - [M_strings:LENGTH] return string padded to at least specified length (LICENSE:PD)
function pad(str,length,pattern,right,clip) result(strout)
character(len=*)                           :: str
integer,intent(in)                         :: length
character(len=max(length,len(trim(line)))) :: strout
character(len=*),intent(in),optional       :: pattern
logical,intent(in),optional                :: right
logical,intent(in),optional                :: clip
pad(3f) pads a string with a pattern to at least the specified length. If the trimmed input string is longer than the requested length the trimmed string is returned.
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. right if true pads string on the right, else on the left clip trim spaces from input string but otherwise retain length. Except for simple cases you typically would trim the input yourself.
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_pad
 use M_strings, only : pad
 implicit none
 character(len=10)            :: string='abcdefghij'
 character(len=:),allocatable :: answer
 integer                      :: i
 character(len=*),parameter   :: g='(*(g0))'
    answer=pad(string,5)
    write(*,'("[",a,"]")') answer
    answer=pad(string,20)
    write(*,'("[",a,"]")') answer
    i=30
    write(*,g)
    write(*,'(1x,a,1x,i0)') &
     & pad('CHAPTER 1 : The beginning ',i,'.'), 1   , &
     & pad('CHAPTER 2 : The end ',i,'.'),       1234, &
     & pad('APPENDIX ',i,'.'),                  1235
    write(*,*)
    write(*,'(1x,a,i7)') &
     & pad('CHAPTER 1 : The beginning ',i,'.'), 1   , &
     & pad('CHAPTER 2 : The end ',i,'.'),       1234, &
     & pad('APPENDIX ',i,'.'),                  1235
     write(*,g)pad('12',5,'0',right=.false.)
     write(*,g)pad('12345 ',30,'_',right=.false.)
     write(*,g)pad('12345 ',30,'_',right=.false.,clip=.true.)
     write(*,g)pad('12345 ',7,'_',right=.false.)
     write(*,g)pad('12345 ',7,'_',right=.false.,clip=.true.)
     write(*,g)pad('12345 ',6,'_',right=.false.)
     write(*,g)pad('12345 ',6,'_',right=.false.,clip=.true.)
     write(*,g)pad('12345 ',5,'_',right=.false.)
     write(*,g)pad('12345 ',5,'_',right=.false.,clip=.true.)
     write(*,g)pad('12345 ',4,'_',right=.false.)
     write(*,g)pad('12345 ',4,'_',right=.false.,clip=.true.)
end program demo_pad
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 00012 ___12345 ____12345 _12345 __12345 12345 _12345 12345 12345 12345 12345
 adjustl(3f), adjustr(3f), repeat(3f), trim(3f), len_trim(3f), len(3f)
 adjustc(3f), stretch(3f), lpad(3f), rpad(3f), cpad(3f), zpad(3f), lenset(3f)
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 | ||
| logical, | intent(in), | optional | :: | right | ||
| logical, | intent(in), | optional | :: | clip | 
function pad(line,length,pattern,right,clip) result(strout)
!$@(#) M_strings::pad(3f): return string padded to at least specified length
character(len=*),intent(in)          :: line
integer,intent(in)                   :: length
character(len=*),intent(in),optional :: pattern
logical,optional,intent(in)          :: right
logical,optional,intent(in)          :: clip
character(len=:),allocatable         :: strout
logical                              :: local_right
logical                              :: local_clip
character(len=:),allocatable         :: local_pattern
character(len=:),allocatable         :: local_line
if(  present(right)    )then;  local_right=right;      else;  local_right=.true.;  endif
if(  present(clip)     )then;  local_clip=clip;        else;  local_clip=.false.;  endif
if(  present(pattern)  )then;  local_pattern=pattern;  else;  local_pattern=' ';   endif
if(len(local_pattern) == 0)then
   strout=line
else
   if(local_clip)then
      local_line=trim(adjustl(line))
      allocate(character(len=max(length,len(local_line))) :: strout)
   else
      local_line=line
      allocate(character(len=max(length,len(line))) :: strout)
   endif
   if(local_right)then
      strout(:)=local_line//repeat(local_pattern,len(strout)/len(local_pattern)+1)
   else
      strout(:)=repeat(local_pattern, ceiling(real(len(strout))/len(local_pattern)))
      strout(max(0,len(strout)-len(local_line))+1:)=local_line
   endif
endif
end function pad