Firefly开源社区
标题:
lua中字符分割和去掉某类字符
[打印本页]
作者:
代祥军
时间:
2015-3-25 09:54
标题:
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
]]--
复制代码
欢迎光临 Firefly开源社区 (https://dev.t-firefly.com/)
Powered by Discuz! X3.1