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
相关推荐
白太岁3 天前
Redis:(3) Lua 与 Redis、基于连接池的 Facade 模式封装
数据库·c++·redis·lua·外观模式
Maguyusi8 天前
go 批量生成c++和lua proto文件
c++·golang·lua·protobuf
foxsen_xia8 天前
Kamailio通过Lua写路由
开发语言·lua·信息与通信
码农周11 天前
nginx + Lua 实现域名访问日志统计
nginx·lua
難釋懷12 天前
Lua脚本解决多条命令原子性问题
开发语言·lua
AI_567815 天前
Postman接口测试提速技巧:批量请求+智能断言实践
测试工具·lua·postman
小王不爱笑13218 天前
Postman 使用教程
测试工具·lua·postman
TracyCoder12319 天前
Redis 进阶之路:探秘事务、Lua 与特殊数据结构
数据结构·redis·lua
星空露珠20 天前
速算24点所有题库公式
开发语言·数据库·算法·游戏·lua
星空露珠20 天前
速算24点检测生成核心lua
开发语言·数据库·算法·游戏·lua