且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Lua中将计算字符串转换为int

更新时间:2023-02-03 08:59:39

tonumber 只能用于实数字符串,不能用于算术表达式.

tonumber can be used only on a string that is a real number, not an arithmetic expression.

您可以加载字符串并运行它:

You can load the string and run it:

x = "5 + 5"
func = assert(load("return " .. x))
y = func()
print(y)

在 Lua 5.1 中,使用 loadstring 而不是 load.

In Lua 5.1, use loadstring instead of load.