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
相关推荐
AI_567816 小时前
Postman接口测试提速技巧:批量请求+智能断言实践
测试工具·lua·postman
小王不爱笑1324 天前
Postman 使用教程
测试工具·lua·postman
TracyCoder1235 天前
Redis 进阶之路:探秘事务、Lua 与特殊数据结构
数据结构·redis·lua
星空露珠6 天前
速算24点所有题库公式
开发语言·数据库·算法·游戏·lua
星空露珠6 天前
速算24点检测生成核心lua
开发语言·数据库·算法·游戏·lua
想做后端的前端8 天前
Lua的热更新
开发语言·lua
澄风9 天前
Redis ZSet+Lua脚本+SpringBoot实战:滑动窗口限流方案从原理到落地
spring boot·redis·lua
Geoking.9 天前
【Redis】Redis 中的 Pipeline 与 Lua 脚本:高性能与原子性的两种武器
redis·lua
剑之所向10 天前
嵌入式之lua脚本
开发语言·junit·lua
plmm烟酒僧10 天前
使用 Lua 进行汽车 UDS 诊断:轻量级脚本化诊断新思路
嵌入式·lua·汽车电子·uds诊断·汽车诊断·can通信·诊断协议