|
lua中字符分割和去掉某类字符
发表于 2015-3-25 09:54:17
浏览:3807
|
回复:0
打印
只看该作者
[复制链接]
楼主
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
- ]]--
复制代码 |
|