用到了lua的操作字符串方法 string.find, string.sub 具体请看代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16-- 用指定字符串切割另一个字符串 local function strSplit(delimeter, str) local find, sub, insert = string.find, string.sub, table.insert local res = {} local start, start_pos, end_pos = 1, 1, 1 while true do start_pos, end_pos = find(str, delimeter, start, true) if not start_pos then break end insert(res, sub(str, start, start_pos - 1)) start = end_pos + 1 end insert(res, sub(str,start)) return res end
调用:
复制代码
1
2
3
4
5
6
7
8
9
10local function print_r(arr) for k,v in pairs(arr) do io.write("'" .. v .. "'" .. "t") end io.write("n") end print_r(strSplit(",", "a,b,c,123,456")) print_r(strSplit(",", "abc,def,,,ghi,")) print_r(strSplit(",", ",,,,,,,,")) print_r(strSplit("---", "abcde---haha---12345"))
复制代码
1
2
3
4'a' 'b' 'c' '123' '456' 'abc' 'def' '' '' 'ghi' '' '' '' '' '' '' '' '' '' '' 'abcde' 'haha' '12345'
That's all!
最后
以上就是懦弱鸭子最近收集整理的关于lua用指定字符串切割另一个字符串的全部内容,更多相关lua用指定字符串切割另一个字符串内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复