Lua string.format 选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1811884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lua string.format options
提问by RCIX
This may seem like a stupid question, but what are the symbols used for string replacement in string.format? can someone point me to a simple example of how to use it?
这似乎是一个愚蠢的问题,但是 string.format 中用于字符串替换的符号是什么?有人可以指出我如何使用它的简单示例吗?
回答by Robert Harvey
string.format in Lua follows the same patterns as Printf in c:
Lua 中的 string.format 与 c 中的 Printf 遵循相同的模式:
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
There are some exceptions, for those see here:
有一些例外,请参见此处:
回答by RBerteig
Chapter 20 of PiLdescribes string.format
near the end:
PiL 的第 20 章string.format
在结尾处描述了:
The function
string.format
is a powerful tool when formatting strings, typically for output. It returns a formatted version of its variable number of arguments following the description given by its first argument, the so-called format string. The format string has rules similar to those of theprintf
function of standard C: It is composed of regular text and directives, which control where and how each argument must be placed in the formatted string.
string.format
在格式化字符串时,该函数是一个强大的工具,通常用于输出。它根据第一个参数(即所谓的格式字符串)给出的描述,返回其可变数量参数的格式化版本。格式字符串的规则类似于printf
标准 C 函数的规则:它由常规文本和指令组成,它们控制每个参数必须放在格式化字符串中的位置和方式。
The Lua Referencesays:
在Lua中引用说:
The format string follows the same rules as the
printf
family of standard C functions. The only differences are that the options/modifiers*
,l
,L
,n
,p
, andh
are not supported and that there is an extra option,q
.
格式字符串遵循与
printf
标准 C 函数系列相同的规则。唯一的区别是选项/修饰符*
,l
,L
,n
,p
,和h
不支持,并且有一个额外的选项,q
。
The function is implemented by str_format()
in strlib.c
which itself interprets the format string, but defers to the C library's implementation of sprintf()
to actually format each field after determining what type of value is expected (string or number, essentially) to correspond to each field.
该功能是通过执行str_format()
在strlib.c
其本身解释该格式串,但是推迟到C库的执行sprintf()
确定的值类型预期什么(字符串或数字,基本上)以对应于每个字段后到实际上格式化每个字段。
回答by Nick Dandoulakis
There should be "Lua Quick Reference" html file in your hard disk, if you used an installation package.
(for example: ../Lua/5.1/docs/luarefv51.html)
如果您使用安装包,您的硬盘中应该有“Lua 快速参考”html 文件。
(例如:../Lua/5.1/docs/luarefv51.html)
There you'll find, among other things,
在那里你会发现,除其他外,
string.format (s [, args ])
string.format (s [, args ])
- Formatting directives
- Formatting field types
- Formatting flags
- Formatting examples
- 格式化指令
- 格式化字段类型
- 格式化标志
- 格式化示例