我有一个 bool 变量,我想以格式化字符串显示其值。我尝试使用 string.format
,但是对于 language reference 中列出的任何格式选项选择,都会得到类似以下内容的内容:
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(string.format("%c\n", true))
stdin:1: bad argument #2 to 'format' (number expected, got boolean)
stack traceback:
[C]: in function 'format'
stdin:1: in main chunk
[C]: ?
我可以通过添加tostring
来显示 bool 值,
> print(string.format("%s\n", tostring(true)))
true
但这对于这个 lua 初学者来说似乎相当间接。是否有我忽略的格式选项?或者我应该使用上面的方法?还有别的吗?
最佳答案
查看string.format
的代码,我没有看到任何支持 bool 值的内容。
我认为在这种情况下 tostring
是最合理的选择。
示例:
print("this is: " .. tostring(true)) -- Prints: this is true
关于lua - 如何使用 bool 变量格式化lua字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6615572/