C Library Functions  - pad (3)

NAME

pad(3f) - [M_strings:LENGTH] return string padded to at least specified length (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Returns
Example
See Also
Author
License

SYNOPSIS

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

DESCRIPTION

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.

OPTIONS

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.

RETURNS

strout The input string padded to the requested length or the trimmed input string if the input string is longer than the requested length.

EXAMPLE

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

SEE ALSO

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)

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 pad (3) July 22, 2023
Generated by manServer 1.08 from 78679d68-fd2c-407f-b163-d308a29ff8d7 using man macros.