Skip to content

Instantly share code, notes, and snippets.

@hanxi
Created November 21, 2014 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanxi/e706943b702f64e14b84 to your computer and use it in GitHub Desktop.
Save hanxi/e706943b702f64e14b84 to your computer and use it in GitHub Desktop.
lua string split
function split(str,sep)
local array = {}
local reg = string.format("([^%s]+)",sep)
local tinsert = table.insert
for mem in string.gmatch(str,reg) do
tinsert(array, mem)
end
return array
end
local s = "one;two;;four"
local array = split(s,";")
for n, w in ipairs(array) do
print(n .. ": " .. w)
end
function split(str,sep)
local array = {}
local reg = string.format("([^%s]+)",sep)
local tinsert = table.insert
string.gsub(str,reg, function(mem)
tinsert(array,mem)
end)
return array
end
local s = "one;two;;four"
local array = split(s,";")
for n, w in ipairs(array) do
print(n .. ": " .. w)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment