获取字符串中第一个空格后的部分

所以假设我有一个叫做 Hi there 的字符串。

我目前正在使用

m:match("^(%S+)")

从字符串中获取 Hi,现在我只需要从字符串中获取 "there",但我不知道该怎么做。

点赞
用户1245380
用户1245380

查看此页面:http://lua-users.org/wiki/SplitJoin

有许多使用空格来拆分字符串的方式。

以下这种方法可能适合您的问题:

function justWords(str)
  local t = {} -- 创建一个空表

  -- 创建一个将单词插入表中的函数
  local function helper(word) table.insert(t, word) return "" end

  -- 获取字符串中的每个单词并将其传递给`helper`
  if not str:gsub("%w+", helper):find"%S" then return t end
end

table = justWords(example)
table[1] -- hi
table[2] -- there
table[3] -- nil
2016-01-26 20:27:57