Created
November 21, 2014 02:24
-
-
Save hanxi/e706943b702f64e14b84 to your computer and use it in GitHub Desktop.
lua string split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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