2024-05-26 16:27:43 发布
网友
Lua是否有任何与python的“in”语句等价的语句
例如:
if "word" in variable:
使用查找
string.find('banana', 'an')
见https://www.lua.org/pil/20.1.html
Lua没有Python的in操作符的等价物,但是如果您知道要模拟的容器类型,那么为它编写函数就很容易了
in
str
true参数确保子字符串按字面意义处理,而不是按模式处理。详情见^{}
true
local function inString(s, substring) return s:find(substring, 1, true) end
list
local function inArray(array, x) for _, v in ipairs(array) do if v == x then return true end end return false end
dict
local function inKeys(t, k) return t[k] ~= nil end
使用查找
见https://www.lua.org/pil/20.1.html
Lua没有Python的
in
操作符的等价物,但是如果您知道要模拟的容器类型,那么为它编写函数就很容易了str
true
参数确保子字符串按字面意义处理,而不是按模式处理。详情见^{list
dict
相关问题 更多 >
编程相关推荐