How to fmt.Print("print this on the center")

huangapple go评论74阅读模式
英文:

How to fmt.Print("print this on the center")

问题

可以使用fmt.Println("...")来在终端中实现字符串的居中对齐打印吗?

英文:

Is possible, with fmt.Println("...") to print a string with center alignement of the shell?

答案1

得分: 13

作为对这个长时间回答的问题的更新,@miltonb发布的解决方案可以通过使用fmt包中的*符号进行改进。根据包文档的说明:

在Printf、Sprintf和Fprintf中,默认行为是每个格式化动词格式化调用中传递的连续参数。然而,动词之前的表示法[n]表示要格式化第n个从1开始索引的参数。**对于宽度或精度,相同的表示法在'*'之前选择保存值的参数索引。**在处理了一个带括号的表达式[n]之后,后续的动词将使用参数n+1、n+2等,除非另有指示。

因此,你可以使用更简洁的格式语句来替换两个fmt.Sprintf调用,以实现相同的结果:

s := "in the middle"
w := 110 // 或其他值

fmt.Sprintf("%[1]*s", -w, fmt.Sprintf("%[1]*s", (w + len(s))/2, s))

在此处查看代码示例

此外,正如@chris-dennis提到的,此示例中的[1]表示法是多余的,因为字符串的宽度已经是第一个参数。这也可以工作:

fmt.Sprintf("%*s", -w, fmt.Sprintf("%*s", (w + len(s))/2, s))

在此处查看此示例的运行结果

英文:

As an update to this long-answered question, the solution posted by @miltonb can be improved upon by using the * notation from the fmt package. From the package documentation:

> In Printf, Sprintf, and Fprintf, the default behavior is for each
> formatting verb to format successive arguments passed in the call.
> However, the notation [n] immediately before the verb indicates that
> the nth one-indexed argument is to be formatted instead. The same
> notation before a '*' for a width or precision selects the argument
> index holding the value.
After processing a bracketed expression [n],
> subsequent verbs will use arguments n+1, n+2, etc. unless otherwise
> directed.

So you can replace two of the fmt.Sprintf calls with a more concise format statement to achieve the same result:

s := "in the middle"
w := 110 // or whatever

fmt.Sprintf("%[1]*s", -w, fmt.Sprintf("%[1]*s", (w + len(s))/2, s))

See the code in action.

Additionally, as @chris-dennis mentioned, the [1] notation is superfluous in this example because the width of the string is already the first argument. This works as well:

fmt.Sprintf("%*s", -w, fmt.Sprintf("%*s", (w + len(s))/2, s))

See this example in action.

答案2

得分: 1

这段代码可以将文本居中显示,前提是已知终端的宽度。虽然不是完全"像素完美",但希望能有所帮助。

如果我们将其分解,可以看到有两部分代码用于生成右侧和左侧的格式化字符串。

fmt.Sprintf("%%-%ds", w/2)  // 生成"%-55s",即左侧填充字符串
fmt.Sprintf("%%%ds", w/2)   // 生成"%55s",即右侧填充字符串

因此,最终的Printf语句如下:

fmt.Printf("%-55s", fmt.Sprintf("%55s", "my string to centre"))

完整的代码如下:

s := " in the middle"
w := 110 // 终端宽度

fmt.Printf(fmt.Sprintf("%%-%ds", w/2), fmt.Sprintf(fmt.Sprintf("%%%ds", w/2),s))

运行结果如下:

                                     in the middle

playground链接:play

英文:

This code is managing to centre the text as long as the shell width is a known value. Its not quite 'pixel perfect', but I hope it helps.

If we break it down there are two bits of code to produce the format strings to pad right and then left.

fmt.Sprintf("%%-%ds", w/2)  // produces "%-55s"  which is pad left string
fmt.Sprintf("%%%ds", w/2)   // produces "%55s"   which is right pad

So the final Printf statement becomes

fmt.Printf("%-55s", fmt.Sprintf("%55s", "my string to centre")) 

The full code:

s := " in the middle"
w := 110 // shell width

fmt.Printf(fmt.Sprintf("%%-%ds", w/2), fmt.Sprintf(fmt.Sprintf("%%%ds", w/2),s))

Produces the following:

> in the middle

Link to play ground: play

huangapple
  • 本文由 发表于 2016年12月14日 08:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/41133006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定