使用变量作为格式指令的前缀参数 [英] Using a variable as a prefix argument to a format directive
问题描述
我需要在它之前打印一些带有可变数量空格的东西.例如,如果我需要在文本前打印 5 个空格,我会这样做:
I need to print something with variable number of spaces before it. For example if I need to print 5 spaces before my text, I will do:
(format T "%5T My Text")
Output: My Text
我可以使用变量来代替 5 并能够将值传递给它吗?我正在寻找的是这样的:
In place of 5, can I use a variable and be able to pass on a value to it? What I am looking for is like:
(format T "%(~d)T My Text" 5)
output: My Text
推荐答案
试试
(format T "~vT My Text" 5)
参见22.3 格式化输出:
可以使用V
(或v
)代替指令的前缀参数.在这种情况下,format 将来自 args 的参数作为参数传递给指示.参数应该是一个整数或字符.如果 argV
参数使用的是nil,效果就好像参数有被省略了.#
可以用来代替前缀参数;它表示剩余要处理的 args 个数.使用时在递归格式中,在 ~?
或 ~{
的上下文中,#
前缀参数表示剩余的格式参数的数量递归调用.
In place of a prefix parameter to a directive,
V
(orv
) can be used. In this case, format takes an argument from args as a parameter to the directive. The argument should be an integer or character. If the arg used by aV
parameter is nil, the effect is as if the parameter had been omitted.#
can be used in place of a prefix parameter; it represents the number of args remaining to be processed. When used within a recursive format, in the context of~?
or~{
, the#
prefix parameter represents the number of format arguments remaining within the recursive call.
这篇关于使用变量作为格式指令的前缀参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!