简体   繁体   English

lua中string.format函数的布尔插值字符是什么

[英]What is the boolean interpolation character for string.format function in lua

I'm looking for a boolean interpolation character for string.format (as the title says). 我正在为string.format寻找一个布尔插值字符(如标题所示)。
I want something that will work this way: 我想要一些可以这样工作的东西:

print(string.format("nil == false: %b",(nil==false))

%b is just a placeholder, you'll get an error with that. %b只是一个占位符,你会得到一个错误。 I'm looking for 'b'. 我正在寻找'b'。 I can't just do: 我不能这样做:

print("nil == false: " .. (nil==false))

because booleans can't be concatenated with strings. 因为布尔值不能与字符串连接。 I could do: 我可以做:

val=(nil==false)
if val==false then truth="false" else truth="true" end
print("nil==false: ".. truth)

But it's too much work. 但是工作太多了。

Well, first you should try reading the relevant section of the manual . 那么,首先你应该尝试阅读手册的相关部分 That will let you discover that there is no format specifier for booleans. 这将让您发现布尔值没有格式说明符。

What greatwolf suggests is a solution, ie converting the value explicitly to a string. 伟大的狼建议的是一种解决方案,即将值明确地转换为字符串。 If there is a possibility that your truth value may be nil , but you want to output it as false , this trick is useful: 如果您的真值可能nil ,但您想将其输出为false ,则此技巧很有用:

truth = nil
print("nil==false: ".. tostring( not not truth ))

In this way both nil and false will be displayed as false . 这样, nilfalse都将显示为false

Edit (to answer a comment) 编辑 (回答评论)

In Lua 5.2 the %s specifier automatically convert the arguments to strings using tostring internally. 在Lua 5.2中, %s说明符在内部使用tostring自动将参数转换为字符串。 Thus: 从而:

print( string.format( "%s  %s  %s", true, nil, {} ) )

prints: 打印:

true  nil  table: 00462400

otherwise you can create your own formatting function wrapping string.format : 否则你可以创建自己的格式化函数包装string.format

 local function myformat( fmt, ... )
    local buf = {}
     for i = 1, select( '#', ... ) do
         local a = select( i, ... )
         if type( a ) ~= 'string' and type( a ) ~= 'number' then
             a = tostring( a )
         end
         buf[i] = a
     end
     return string.format( fmt, unpack( buf ) )
 end


 print( myformat( "%s  %s  %s", true, nil, {} ) )

If you're wondering how to modify string.format so it supports bools, here's one way you can do it: 如果您想知道如何修改string.format以便它支持bools,这里有一种方法可以做到:

do
local format = string.format
function string.format(str, ...)
  local args = {...}
  local boolargs = {}
  str = str:gsub("%%b", "%%%%b")
  for i = #args, 1, -1 do
    if type(args[i]) == "boolean" then
      table.insert(boolargs, 1, args[i])
      table.remove(args, i)
    end
  end
  str = format(str, unpack(args))

  local j = 0
  return (str:gsub("%%b", function(spec) j = j + 1; return tostring(boolargs[j]) end))
end
end

print(string.format("%s is %b", "nil == false", nil==false))

It might be a bit confusing to follow. 跟随它可能有点令人困惑。 The idea is to gsub all "%b" in the string and replace it with double escape %%b so format doesn't try to interpret it. 想法是在字符串中gsub所有“%b”并用双重转义%%b替换它,因此格式不会尝试解释它。 We let string.format do its stuff and we take the result and handle %b manually ourselves. 我们让string.format做它的东西,然后我们自己处理结果并手动处理%b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM