Lua string.format选项

Lua string.format options

这似乎是一个愚蠢的问题,但是string.format中用于替换字符串的符号是什么? 有人可以给我指出一个简单的用法示例吗?


Lua中的string.format与c中的Printf遵循相同的模式:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

有一些例外,请参见此处:

http://pgl.yoyo.org/luai/i/string.format


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 the printf 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.

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, and h are not supported and that
there is an extra option, q.

该函数由strlib.c中的str_format()实现,它本身会解释格式字符串,但根据C库的sprintf()实现,可以在确定期望的值类型(实际上是字符串或数字)之后对每个字段进行实际格式化对应每个字段。


如果使用安装包,则硬盘中应该有" Lua快速参考" html文件。
(例如:../ Lua / 5.1 / docs / luarefv51.html)

在那里,您会发现,

string.format (s [, args ])

  • 格式指令
  • 格式化字段类型
  • 格式化标志
  • 格式化示例