格式:在字符输出中添加尾随空格以左对齐 [英] Format: add trailing spaces to character output to left-justify

查看:113
本文介绍了格式:在字符输出中添加尾随空格以左对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何格式化字符串以使其具有恒定宽度并且左对齐?有Aw格式器,其中w表示所需的字符输出宽度,但是如果w > len(characters),它将加空格,而不是附加空格.

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 character output, but it prepends the spaces if w > len(characters), instead of appending them.

当我尝试

44 format(A15)
   print 44, 'Hi Stack Overflow'

我知道

>        Hi Stack Overflow<

代替

>Hi Stack Overflow        <

有没有简单的Fortran格式解决方案可以解决此问题?

Is there any simple Fortran formatting solution that solves this?

推荐答案

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

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')

该功能留给读者练习.

这篇关于格式:在字符输出中添加尾随空格以左对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆