格式:在字符输出中添加尾随空格以左对齐
Format: add trailing spaces to character output to left-justify
如何将字符串格式化为具有恒定宽度 并且 左对齐?有 Aw
格式化程序,其中 w
表示字符输出的所需宽度,但如果 w > len(characters)
,它会在前面加上空格,而不是附加它们。
当我尝试时
44 format(A15)
print 44, 'Hi Stack Overflow'
我明白了
> Hi Stack Overflow<
而不是
>Hi Stack Overflow <
是否有任何简单的 Fortran 格式化解决方案可以解决这个问题?
如问题中所述,问题是当字符表达式的长度短于输出字段宽度时,填充空格出现在 之前 字符表达式。我们想要的是在我们想要的字符串之后填充空格。
就自然编辑描述符而言,没有简单的格式化解决方案。但是,我们可以做的是输出一个具有足够尾随空格(计入长度)的表达式。
例如:
print '(A50)', 'Hello'//REPEAT(' ',50)
或
character(50) :: hello='Hello'
print '(A50)', hello
甚至
print '(A50)', [character(50) :: 'hello']
也就是说,在每种情况下,输出项都是一个长度(至少)50 的字符。每个都将在右边用空格填充。
如果您愿意,您甚至可以创建一个函数,其中 returns 扩展(左对齐)表达式:
print '(A50)', right_pad('Hello')
其中函数留作 reader 的练习。
有点难看,但你可以连接一个空字符串:
character*15 :: blank=' '
print 44, 'Hi Stack Overflow'//blank
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()
将前导空格移动到字符串的末尾。
建议不要限制输出字数。
将其更改为以下内容即可:
44 format(A)
print 44, 'Hi Stack Overflow'
为了完成@francescalus 出色的答案以供将来参考,建议的解决方案也适用于可分配的字符串文字:
character(len=:), allocatable :: a_str
a_str = "foobar"
write (*,"(A,I4)") a_str, 42
write (*,"(A,I4)") [character(len=20) :: a_str], 42
会输出
foobar 42
foobar 42
如何将字符串格式化为具有恒定宽度 并且 左对齐?有 Aw
格式化程序,其中 w
表示字符输出的所需宽度,但如果 w > len(characters)
,它会在前面加上空格,而不是附加它们。
当我尝试时
44 format(A15)
print 44, 'Hi Stack Overflow'
我明白了
> Hi Stack Overflow<
而不是
>Hi Stack Overflow <
是否有任何简单的 Fortran 格式化解决方案可以解决这个问题?
如问题中所述,问题是当字符表达式的长度短于输出字段宽度时,填充空格出现在 之前 字符表达式。我们想要的是在我们想要的字符串之后填充空格。
就自然编辑描述符而言,没有简单的格式化解决方案。但是,我们可以做的是输出一个具有足够尾随空格(计入长度)的表达式。
例如:
print '(A50)', 'Hello'//REPEAT(' ',50)
或
character(50) :: hello='Hello'
print '(A50)', hello
甚至
print '(A50)', [character(50) :: 'hello']
也就是说,在每种情况下,输出项都是一个长度(至少)50 的字符。每个都将在右边用空格填充。
如果您愿意,您甚至可以创建一个函数,其中 returns 扩展(左对齐)表达式:
print '(A50)', right_pad('Hello')
其中函数留作 reader 的练习。
有点难看,但你可以连接一个空字符串:
character*15 :: blank=' '
print 44, 'Hi Stack Overflow'//blank
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()
将前导空格移动到字符串的末尾。
建议不要限制输出字数。
将其更改为以下内容即可:
44 format(A)
print 44, 'Hi Stack Overflow'
为了完成@francescalus 出色的答案以供将来参考,建议的解决方案也适用于可分配的字符串文字:
character(len=:), allocatable :: a_str
a_str = "foobar"
write (*,"(A,I4)") a_str, 42
write (*,"(A,I4)") [character(len=20) :: a_str], 42
会输出
foobar 42
foobar 42