【问题标题】:how can i get all the text that comes after a words我怎样才能得到单词后面的所有文字
【发布时间】:2021-03-24 10:30:03
【问题描述】:

到目前为止我所拥有的:

local msg = "hello hi hey"
local words = {}
for word in msg:gmatch("%S+") do
    table.insert(words, word)
end
-- splits msg by every blank space and drops it in words

我想去掉 msg 中的第一个单词,并将其余单词保存为变量。我发现我可以使用table.remove(words, 1) 来删除第一个单词,但是如何将其余单词保存为变量?

【问题讨论】:

标签: lua


【解决方案1】:

所以lua split into words的答案

s = "foo bar 123"
words = {}
for word in s:gmatch("%w+") do table.insert(words, word) end

还有你的代码:

table.remove(words, 1) 

那么你有你的结果:

> words[1]
bar
> words[2]
123

编辑:如果你想连接剩余的字符串:

> words={"test", "123"}
> variable = table.concat(words, " ")
> variable
test 123
> type(variable)
string
>

【讨论】:

  • 这不能回答我的问题
  • 可能我理解错了,但是get rid of the first word, but how can I save the rest as a variable,你用remove去掉第一个单词,剩下的都保存在变量words中。您想要的是连接字符串中的单词吗?
  • 是的,感谢您的编辑解决了我的问题
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2018-05-17
  • 1970-01-01
  • 2018-02-22
  • 2023-03-22
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
相关资源
最近更新 更多