lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Jan 18, 2010 at 12:41 AM, Vaughan McAlley wrote:
> Could string.split() be like string.gmatch(), but just return what
> isn’t matched (ie what lies between the matches)?

That's a good insight: They are in some ways complements of each
other, especially if you implement string.split to return an iterator
as well, as does Rici's implementation [1].

Note that

  for w in (","):gmatch(",") do print("[" .. w .. "]") end  --> [,]
  for w in ("a"):gmatch("") do print("[" .. w .. "]") end  --> [] []

provides a further justification imposing this behavior [2]

  for w in (","):split(",") do print("[" .. w .. "]") end  --> [] []
  for w in ("a"):split("") do print("[" .. w .. "]") end  --> [] [a]
[] (not [a])

[1] http://lua-users.org/lists/lua-l/2006-12/msg00414.html
[2] http://lua-users.org/lists/lua-l/2009-12/msg00952.html