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
相关推荐
石头wang3 小时前
postman如何设置鉴权authorization header(怎么只设置一次,统一管理,不要每个request重复设置)
测试工具·lua·postman
qq_348231851 天前
Redis 事务(MULTI/EXEC)与 Lua 脚本的核心区别
数据库·redis·lua
没有腰的嘟嘟嘟1 天前
从 0 到 1:我如何用 Spring Boot 3 + Redis 打造一个生产级通用幂等与防重中间件(含图解 + 代码 + 案例)
spring boot·redis·中间件·lua
小雨下雨的雨2 天前
第5篇:Redis事务与Lua脚本
redis·junit·lua
BuHuaX2 天前
Lua入门
开发语言·unity·junit·c#·游戏引擎·lua
根哥的博客3 天前
编译nginx-1.28.0支持lua语法
nginx·lua·openresty·nosql注入漏洞
8Qi83 天前
Redis之Lua脚本与分布式锁改造
java·redis·分布式·lua
IMPYLH4 天前
Lua 的 tonumber 函数
开发语言·笔记·后端·junit·游戏引擎·lua
想做后端的前端4 天前
Lua基础语法
junit·单元测试·lua
IMPYLH4 天前
Lua 的 type 函数
开发语言·笔记·后端·junit·lua