lua判断子串,字符串替换,字符串分割

故事背景,需要在聊天做一个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
相关推荐
lanhuazui103 分钟前
lua中 string.match返回值
lua
无名客06 分钟前
redis分布式锁为什么采用Lua脚本实现。而不是事务
redis·分布式·lua·事务
我发在否1 天前
Lua > OpenResty HelloWorld
lua·openresty
测试界清流1 天前
postman接口功能测试
开发语言·lua
lanhuazui102 天前
lua中table键类型及lua中table的初始化有几种方式
lua
程序猿多布3 天前
Lua和C#比较
c#·lua
我发在否3 天前
Lua > Mac Mini M4安装openresty
macos·lua·openresty
Aiah.3 天前
大彩串口屏-烧录与调试
lua·大彩串口屏·ui显示
程序猿多布3 天前
Lua 面向对象编程
lua
qyvlik3 天前
MacOS 使用 luarocks+wrk+luajit
macos·lua·wrk