【问题标题】:Lua string split for 5.2Lua 字符串拆分为 5.2
【发布时间】:2014-01-18 05:15:42
【问题描述】:

我认为这是 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.的原始问题的链接

【问题讨论】:

  • 我相信你误诊了这个问题。请参阅我对 Schollii 对您的其他问题的回答的评论(以及他们最近的评论,这与我的相同)。

标签: string lua split lua-table


【解决方案1】:

从我的 POV 来看,该拆分功能不会有任何问题。

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

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

【讨论】:

  • 我将接受这个答案并得出结论,在 5.2 中使用此 5.1 实用程序没有问题。感谢您的意见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 2010-11-28
  • 2018-06-01
  • 2016-08-25
  • 2021-06-10
  • 2016-11-11
相关资源
最近更新 更多