我是靠谱客的博主 懦弱鸭子,这篇文章主要介绍lua用指定字符串切割另一个字符串,现在分享给大家,希望可以做个参考。

用到了lua的操作字符串方法 string.find, string.sub 具体请看代码


-- 用指定字符串切割另一个字符串
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

调用:

local 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"))



输出:


'a'	'b'	'c'	'123'	'456'
'abc'	'def'	''	''	'ghi'	''
''	''	''	''	''	''	''	''	''
'abcde'	'haha'	'12345'	


That's all!

最后

以上就是懦弱鸭子最近收集整理的关于lua用指定字符串切割另一个字符串的全部内容,更多相关lua用指定字符串切割另一个字符串内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(105)

评论列表共有 0 条评论

立即
投稿
返回
顶部