Using a variable as a prefix argument to a format directive

梦想与她 提交于 2019-12-04 03:31:46

问题


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

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

回答1:


Try

(format T "~vT My Text" 5)

See 22.3 Formatted Output:

In place of a prefix parameter to a directive, V (or v) 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 a V 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.



来源:https://stackoverflow.com/questions/19892186/using-a-variable-as-a-prefix-argument-to-a-format-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!