zpad(3f) - [M_strings:LENGTH] pad a string on the left with zeros to
specified length
(LICENSE:PD)
function zpad(valuein,length) result(strout)
class*,intent(in) :: valuein(..)
integer,intent(in),optional :: length
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 at least
the specified length. If the trimmed input string is longer than the
requested length the original string is returned trimmed of leading
and trailing spaces.
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. If the requested length is exceeded
the returned string is untruncated but cropped of leading and trailing
spaces.
str May be a scalor or vector string or integer. The input string
to return trimmed, but then padded to the specified length
if shorter than length. If an integer is input it is first
converted to a string. 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 STR is used. If the input value
STR is not a string no zero padding occurs if LENGTH is not
supplied.
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_zpad
use M_strings, only : zpad
implicit none
integer :: lun, i
write(*,'("[",a,"]")') zpad( '111', 5)
write(*,'("[",a,"]")') zpad( '123456789', 5)
write(*,'("[",a,"]")') zpad( ' 34567 ', 7)
write(*,'("[",a,"]")') zpad( valuein=42 , length=7)
write(*,'("[",a,"]")') zpad( ' +34567 ', 7)
write(*,'("[",a,"]")') zpad( ' -34567 ', 7)
write(*,'("[",a,"]")') zpad(1234)
write(*,'("[",a,"]")') zpad(-1234)
write(*,'("[",a,"]")') zpad(1234,8)
write(*,'("[",a,"]")') zpad(-1234,8)
write(*,'("[",a,"]")') zpad('')
write(*,'("[",a,"]")') zpad('0')
write(*,'("[",a,"]")') zpad('0 ')
write(*,'("[",a,"]")') zpad(' ')
write(*,'("[",a,"]")') zpad([1,10,100,1000,10000,100000],8)
! 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]
[123456789]
[0034567]
[0000042]
[+0034567]
[-0034567]
[1234]
[-1234]
[00001234]
[-00001234]
[]
[0]
[00000]
[00000]
[00000001]
[00000010]
[00000100]
[00001000]
[00010000]
[00100000]
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(*), | intent(in) | :: | valuein | |||
integer, | intent(in), | optional | :: | length |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(*), | intent(in) | :: | valuein(:) | |||
integer, | intent(in), | optional | :: | length |