C Library Functions  - zpad (3)

NAME

zpad(3f) - [M_strings:LENGTH] pad a string on the left with zeros to specified length (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Returns
Example
Author
License

SYNOPSIS

function zpad(valuein,length) result(strout)

    class*,intent(in)           :: valuein(..)
    integer,intent(in),optional :: length

DESCRIPTION

zpad(3f) crops the input string (or integer, which will be converted to a string) and then pads it on the left with zeros to the specified length.

Note that if the trimmed input string is already as long or longer than the requested length the trimmed original string is returned.

For strings representing unsigned numbers this is basically an alias for

       strout=pad(str,length,’0’,clip=.true.,right=.false.)

For integers the same is often done with internal WRITE(3f) statements such as

       write(strout,’(i5.5)’)ivalue

but unlike internal I/O the function call can be used in expressions or passed as a procedure argument.

OPTIONS

valuein
  The input value to left-pad. May be a scalar or vector string or integer. If the leftmost non-blank character is a sign character it is moved to the left-most position of the output.
length The minimum string length to return. If not present, the length of the input parameter VALUEIN is used. If the input value VALUEIN is an integer no zero padding occurs if LENGTH is not supplied.

RETURNS

strout A trimmed string padded on the left with zeros to the requested length

EXAMPLE

Sample Program:

     program demo_zpad
      use M_strings, only : zpad
      implicit none
      character(len=*),parameter :: boxed=’("[",a,"]",*(g0,1x))’
      integer :: lun, i
         print boxed, zpad( ’111’, 5),’basic use’
         print boxed, zpad( valuein=42 , length=7),’by argument name’
         print boxed, zpad( ’  34567  ’, 7),’cropped before padding’
         print boxed, zpad( ’123456789’, 5),’input longer than length’
         print boxed, zpad( ’  +34567  ’, 7),’starts with plus sign’
         print boxed, zpad( ’  -34567  ’, 7),’starts with minus sign’
         print boxed, zpad(1234),’some integers instead of strings’
         print boxed, zpad(-1234)
         print boxed, zpad(1234,8)
         print boxed, zpad(-1234,8)
         print boxed, zpad(’’),’a null gets you nothing’
         print boxed, zpad(’0’),’but blanks are used for default length’
         print boxed, zpad(’0    ’)
         print boxed, zpad(’     ’)
         print *, ’input value may be an array:’
         print ’("[",a,"]")’, zpad([1,10,100,1000,10000,100000],8)

! example usage: ! open output_00085.dat i=85 open(newunit=lun,file=’output_’//zpad(i,5)//’.dat’) close(unit=lun,status=’delete’)

end program demo_zpad

Results:

    > [00111]basic use
    > [0000042]by argument name
    > [0034567]cropped before padding
    > [123456789]input longer than length
    > [+0034567]starts with plus sign
    > [-0034567]starts with minus sign
    > [1234]some integers instead of strings
    > [-1234]
    > [00001234]
    > [-00001234]
    > []a null gets you nothing
    > [0]but blanks are used for default length
    > [00000]
    > [00000]
    >  input value may be an array:
    > [00000001]
    > [00000010]
    > [00000100]
    > [00001000]
    > [00010000]
    > [00100000]

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 zpad (3) July 22, 2023
Generated by manServer 1.08 from e448c973-b428-4113-8cc5-100b5f2a84ff using man macros.