Lua字符串拆分为5.2

时间:2014-01-15 16:13:11

标签: string lua split lua-table

我遇到的问题是我认为5.1这个常用的字符串拆分功能:

utils = {
split = function(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
     table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end
}

我正在使用Lua版本5.2,我想知道是否有人知道或知道5.2的字符串拆分功能,或者他们是否可以确认或否认此代码是否会遇到5.2中运行的问题?这是reference.

原始问题的链接

1 个答案:

答案 0 :(得分:3)

我的POV中的分割功能没有问题。

注意将添加注释,因为旧的(5.0)表长度语法。 http://www.lua.org/pil/19.1.html

没有什么可以导致这种拆分实现中的错误(它已知的实用函数,我在多个5.2项目中使用过,从来没有任何问题)