Format: add trailing spaces to character output to left-justify

前端 未结 5 1974
渐次进展
渐次进展 2021-01-06 11:34

How do you format a string to have constant width and be left-justified? There is the Aw formatter, where w denotes desired width of chara

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 12:11

    To complete @francescalus excellent answer for future reference, the proposed solution also works in case of allocatables in place of string literals:

    character(len=:), allocatable :: a_str
    
    a_str = "foobar"
    
    write (*,"(A,I4)") a_str, 42
    write (*,"(A,I4)") [character(len=20) :: a_str], 42
    

    will output

    foobar  42
    foobar                42
    

提交回复
热议问题