Skip to content

Instantly share code, notes, and snippets.

@cutthroat
Created January 28, 2013 16:02
Show Gist options
  • Save cutthroat/4656721 to your computer and use it in GitHub Desktop.
Save cutthroat/4656721 to your computer and use it in GitHub Desktop.
Split strings in Lua, the Python way
function string.split(s, pattern, init, plain)
local function f(s, init)
local n = #s + 1
if init > n then return nil end
local i, j = s:find(pattern, init, plain)
if i == nil then
return n + 1, init, n - 1
else
return j + 1, init, i - 1
end
end
return f, s, init or 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment