2016-01-26 36 views
2

所以,假設我有一個名爲Hi的字符串。獲得空白後的字符串的一部分

我目前使用

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

從字符串得到的只是嗨,現在我需要做的只是得到「有」,從字符串,但我不知道怎麼樣。

+0

'M:比賽 '%F [%S] *'' –

回答

2

結帳此頁:http://lua-users.org/wiki/SplitJoin

有許多方法在空白的字符串分開單詞。

這一次似乎像它可能是一個很好的適合你的問題:

function justWords(str) 
    local t = {} -- create an empty table 

    -- create a function to insert a word into the table 
    local function helper(word) table.insert(t, word) return "" end 

    -- grab each word in the string and pass it to `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 
相關問題