lua中字符分割和去掉某类字符

1.lua 以某个字符分割字符串

lua代码:

function LuaSplit(str,split)
    local lcSubStrTab = {}
    while true do
        local lcPos = string.find(str,split)
        if not lcPos then
            lcSubStrTab[#lcSubStrTab+1] =  str    
            break
        end
        local lcSubStr  = string.sub(str,1,lcPos-1)
        lcSubStrTab[#lcSubStrTab+1] = lcSubStr
        str = string.sub(str,lcPos+1,#str)
    end
    return lcSubStrTab
end

local s = LuaSplit("11:22:33:44",":")
local i = 1
while true  do
    if s[i] then
        print(s[i])
        i = i+1
    else 
        break
    end
end
--[[
11
22
33
44
]]--

2.lua 去掉某个字符

lua代码

function LuaReomve(str,remove)
    local lcSubStrTab = {}
    while true do
        local lcPos = string.find(str,remove)
        if not lcPos then
            lcSubStrTab[#lcSubStrTab+1] =  str    
            break
        end
        local lcSubStr  = string.sub(str,1,lcPos-1)
        lcSubStrTab[#lcSubStrTab+1] = lcSubStr
        str = string.sub(str,lcPos+1,#str)
    end
    local lcMergeStr =""
    local lci = 1
    while true do
        if lcSubStrTab[lci] then
            lcMergeStr = lcMergeStr .. lcSubStrTab[lci] 
            lci = lci + 1
        else 
            break
        end
    end
    return lcMergeStr
end
local s = LuaReomve("11:22:33:44",":")
print(s)
--[[
11223344
]]--



你可能感兴趣的:(OpenWrt)