[英]What is the Lua equivalent to python's "in" statement?
Does Lua have any statement equivalent to the "in" statement of python? Lua 是否有任何等同于 python 的“in”语句的语句?
Example:例子:
if "word" in variable:
Lua has no equivalent of Python's in
operator, but if you know what container type you want to emulate, it's easy to write a function for it. Lua 没有与 Python 的in
操作符等效,但是如果您知道要模拟的容器类型,则很容易为它编写函数。
str
The true
argument ensures that the substring is treated literally and not as a pattern. true
参数确保子字符串按字面意思处理,而不是作为模式处理。 See string.find
for details.有关详细信息,请参阅string.find
。
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
Use find使用查找
string.find('banana', 'an')
see https://www.lua.org/pil/20.1.html见https://www.lua.org/pil/20.1.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.