且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

格式:在字符输出中添加尾随空格以左对齐

更新时间:2023-11-30 11:11:34

如问题中所述,问题是,当长度小于输出字段宽度的字符表达式出现时,在之前填充空格>字符表达.我们想要的是让空格放在我们想要的字符串之后.

As noted in the question, the problem is that when a character expression of length shorter than the output field width the padding spaces appear before the character expression. What we want is for the padding spaces to come after our desired string.

就自然的编辑描述符而言,没有简单的 formatting 解决方案.但是,我们可以做的是输出一个带有足够尾随空格的表达式(该长度计入长度).

There isn't a simple formatting solution, in the sense of a natural edit descriptor. However, what we can do is output an expression with sufficient trailing spaces (which count towards the length).

例如:

print '(A50)', 'Hello'//REPEAT(' ',50)

character(50) :: hello='Hello'
print '(A50)', hello

甚至

print '(A50)', [character(50) :: 'hello']

也就是说,在每种情况下,输出项都是一个(至少)长度为50的字符.每个项都将在右侧用空格填充.

That is, in each case the output item is a character of length (at least) 50. Each will be padded on the right with blanks.

如果选择,甚至可以创建一个函数来返回扩展的(左对齐)表达式:

If you chose, you could even make a function which returns the extended (left-justified) expression:

print '(A50)', right_pad('Hello')

该功能留给读者练习.