-
Notifications
You must be signed in to change notification settings - Fork 192
Closed
Labels
easyDifficulty level is easy and good for starting into this projectDifficulty level is easy and good for starting into this projectgood first issueGood for newcomersGood for newcomersideaProposition of an idea and opening an issue to discuss itProposition of an idea and opening an issue to discuss ittopic: stringsString processingString processing
Description
Description
A function to pad a string with zeros or another symbol to a given width could be helpful in many cases, especially together with the to_string()
function for integer to string conversion.
Currently available methods to achieve this are using concatenation
width = 32
str = "hello"
padded_str = repeat('0',width-len(str)')//str
and perhaps also internal file I/O
write(buffer,'(A32)', blank='zero') "hello" ! doesn't work for some reason
I propose to add the following functions
! left - padding
function padl(str,width,symbol) result(padded_str)
character(len=*), intent(in) :: str
integer, intent(in) :: width
character(len=1), intent(in), optional :: symbol
character(len=max(len(str),width) :: padded_str
end function
! right - padding
function padr(str,width,symbol) result(padded_str)
character(len=*), intent(in) :: str
integer, intent(in) :: width
character(len=1), intent(in), optional :: symbol
character(len=max(len(str),width) :: padded_str
end function
The original string is returned if width < len(str)
. The default padding symbol could be either the blank symbol ' '
or '0'
. Special treatment of a leading sign symbol '+'
/'-'
might be also desirable.
Prior art
awvwgk, milancurcic and 14NGiestas
Metadata
Metadata
Assignees
Labels
easyDifficulty level is easy and good for starting into this projectDifficulty level is easy and good for starting into this projectgood first issueGood for newcomersGood for newcomersideaProposition of an idea and opening an issue to discuss itProposition of an idea and opening an issue to discuss ittopic: stringsString processingString processing