string.find Lua 中字典中的一个字符串
string.find a string in a dictionary in Lua
所以我有一本字典:
local toFind = "Layla"
local songs = {
{name = "Eric Clapton - Layla", id = "123"},
{name = "Eric Clapton - I Shot The Sheriff", id = "321"}
}
是否可以使用 string.find(songs, toFind)
或类似的东西找到包含“Layla”字符串的 table?
所有字符串都附加了字符串函数作为方法。
因此你可以直接做...
for k,v in pairs(songs) do
if v.name:find(toFind) then
return songs[k], v.name
end
end
...returns table 和全文...
table: 0x565cbec0 Eric Clapton - Layla
所以我有一本字典:
local toFind = "Layla"
local songs = {
{name = "Eric Clapton - Layla", id = "123"},
{name = "Eric Clapton - I Shot The Sheriff", id = "321"}
}
是否可以使用 string.find(songs, toFind)
或类似的东西找到包含“Layla”字符串的 table?
所有字符串都附加了字符串函数作为方法。
因此你可以直接做...
for k,v in pairs(songs) do
if v.name:find(toFind) then
return songs[k], v.name
end
end
...returns table 和全文...
table: 0x565cbec0 Eric Clapton - Layla