查看字符串中的空格分离代码是否匹配表LUA中的任何对象

发布于 2025-02-12 15:22:18 字数 177 浏览 0 评论 0原文

我有一个字符串,它是“或le pdc”之类的空格分离序列。该代码需要检查代码是否与表中的任何对象匹配(例如{“ r”,“ BE”,“ CRE”})返回true,如果不是,请返回false。在这里,它将返回真实,因为“ R”存在。

我完全很困难,不知道如何解决这个问题。

I have a string which is a space-seperated sequence of codes such as "O R LE Pdc". The code needs to check if a code matches any object in a table (such as {"R", "BE", "Cre"}) return true, if not, return false. Here, it would return true because "R" exists.

I am completely stumped and do not know how to solve this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

德意的啸 2025-02-19 15:22:18
-- the table you're searching through
local t = {"R", "BE", "Cre"}
-- the text you're searching for
local txt = "O R LE Pdc"

-- now create a look-up table
local lut = {}
for _,v in ipairs(t) do
  lut[v] = true
end

-- for every "word" in your string, check if you find it in the look up table
for str in txt:gmatch("%w+") do
  if lut[str] then print("found it") end
end

或者:

for _,v in ipairs(t) do
   if txt:find(v) then print("found it") end
end

for _, v in ipairs(t) do
  for w in txt:gsub("%w+") do
    if v == w then print("found it") end
  end
end

其他许多方法。选择哪一个将取决于您正在处理的数据量。

-- the table you're searching through
local t = {"R", "BE", "Cre"}
-- the text you're searching for
local txt = "O R LE Pdc"

-- now create a look-up table
local lut = {}
for _,v in ipairs(t) do
  lut[v] = true
end

-- for every "word" in your string, check if you find it in the look up table
for str in txt:gmatch("%w+") do
  if lut[str] then print("found it") end
end

Alternatively:

for _,v in ipairs(t) do
   if txt:find(v) then print("found it") end
end

Or

for _, v in ipairs(t) do
  for w in txt:gsub("%w+") do
    if v == w then print("found it") end
  end
end

There are many other ways. Which one to choose will depend on the amount of data you're processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文