gpt4 book ai didi

string - Lua中如何从字符串中取出1个单词

转载 作者:行者123 更新时间:2023-12-03 09:24:18 25 4
gpt4 key购买 nike

我知道如何拆分整个字符串将其放入表格,但是我只需要取出第一个单词,然后字符串的其余部分需要保持不变。

我尝试对此做一些事情,但我不知道:

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

最佳答案

要匹配一个单词,您应该使用 string.match 而不是 string.gmatch:

local words = {}
words[1], words[2] = s:match("(%w+)(.+)")

words[1] 包含第一个单词,words[2] 包含其余单词。

关于string - Lua中如何从字符串中取出1个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24603611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com