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
相关推荐
java搬砖工-苤-初心不变4 天前
基于 lua_shared_dict 的本地内存限流实现
开发语言·junit·lua
程序猿多布4 天前
XLua教程之热补丁技术
unity·c#·lua·xlua
java搬砖工-苤-初心不变4 天前
OpenResty 限流方案对比:lua_shared_dict vs Redis
redis·lua·openresty
程序猿多布4 天前
XLua教程之Lua调用C#
unity·c#·lua·xlua
夜猫逐梦5 天前
【Lua】Windows 下编写 C 扩展模块:VS 编译与 Lua 调用全流程
c语言·windows·lua
java搬砖工-苤-初心不变5 天前
OpenResty 配合 Lua 脚本的使用
开发语言·lua·openresty
半夏知半秋6 天前
基于跳跃表的zset实现解析(lua版)
服务器·开发语言·redis·学习·lua
Wyc724096 天前
Lua语言基础笔记
开发语言·笔记·lua
锐策9 天前
Lua 核心知识点详解
开发语言·lua
lanhuazui109 天前
lua中 string.match返回值
lua