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

前端 未结 5 2090
渐次进展
渐次进展 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:13

    program test ! Write left justified constant width character columns 
    ! declare some strings.
    character(len=32) :: str1,str2,str3,str4,str5,str6 
    ! define the string values.
    str1 = "     Nina "; str2 = "       Alba  " ; str3 = "        blue   " 
    str4 = "  Jamil   "; str5 = "   Arnost "    ; str6 = " green             "
    write(*,'(a)') "123456789012345678901234567890"
    ! format to 3 columns 10 character wide each.
    ! and adjust the stings to the left.
    write(*,'(3(a10))') adjustl(str1), adjustl(str2), adjustl(str3) 
    write(*,'(3(a10))') adjustl(str4), adjustl(str5), adjustl(str6) 
    end program test
    
     $ ./a.out
    123456789012345678901234567890
    Nina      Alba      blue 
    Jamil     Arnost    green
    

    adjustL() moves leading spaces to the end of the string.

提交回复
热议问题