-
string.find
string.gsub 第三个参数可以为table或函数
string.match
string.gmatch
使用find来实现一个我们自己的gmatch,功能和gmatch是差不多的
local str = "Hello World" -- 使用find来实现一个自己的gmatch local myGmatch = function (s, pattern) local resultTb = {} -- 相当于迭代器中的恒定状态 local index = 0 -- 相当于迭代器中的控制变量 local i, j = string.find(s, pattern) while i do resultTb[#resultTb + 1] = string.sub(s, i, j) i, j = string.find(s, pattern, j + 1) end return function () -- 返回一个迭代器函数 index = index + 1 return resultTb[index] end end for i in myGmatch(str, "%a+") do print(i) end
源:http://cn.cocos2d-x.org/tutorial/show?id=1332
Lua字符串重点函数
最新推荐文章于 2024-07-25 18:22:47 发布