故事背景,需要在聊天做一个gm命名,如果输入的字符串带有'@',则根据后面的参数调用函数。匹配函数的时候需要去掉"@"。字符串还需要切割成函数名和参数。
Lua
function string.split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
local content = "@additem 1 1"
if content:find("@") then
print("gm")
end
local content = string.gsub(content, "@", '')
print(content)
local t = string.split(content," ")
for k,v in pairs(t)do
print(v)
end