pad(3f) - [M_strings:LENGTH] return string padded to at least specified length (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
See Also
Author
License
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,.), 1235Results: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
> [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
Nemo Release 3.1 | pad (3m_strings) | January 10, 2025 |