gsub 功能
不要與 string.sub 函式混淆,後者返回一個子字串!
這個怎麼運作
字串引數
("hello world"):gsub("o", "0")
--> returns "hell0 w0rld", 2
-- the 2 means that 2 substrings have been replaced (the 2 Os)
("hello world, how are you?"):gsub("[^%s]+", "word")
--> returns "word word, word word word?", 5
("hello world"):gsub("([^%s])([^%s]*)", "%2%1")
--> returns "elloh orldw", 2
函式引數
local word = "[^%s]+"
function func(str)
if str:sub(1,1):lower()=="h" then
return str
else
return "no_h"
end
end
("hello world"):gsub(word, func)
--> returns "hello no_h", 2
表引數
local word = "[^%s]+"
sub = {}
sub["hello"] = "g'day"
sub["world"] = "m8"
("hello world"):gsub(word, sub)
--> returns "g'day m8"
("hello world, how are you?"):gsub(word, sub)
--> returns "g'day m8, how are you?"
-- words that are not in the table are simply ignored