lua

How to convert string to boolean in lua


I have data received from another script where the variable has the value "false" or "true". I want to convert this value to true or false in lua datatype. Currently, I can do this long way:

if value == "false" then
  value=false
elseif value == "true" then
  value=true
end

Is there a simplest way to convert this like converting string to integer tonumber("1")


Solution

  • You can also do this:

    stringtoboolean={ ["true"]=true, ["false"]=false }
    print(stringtoboolean[s])