Advertisement
billysback

split function

Jun 24th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.53 KB | None | 0 0
  1. local function split(str, pat)
  2.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  3.     if str ~= nil then
  4.        local fpat = "(.-)" .. pat
  5.        local last_end = 1
  6.        local s, e, cap = str:find(fpat, 1)
  7.        while s do
  8.           if s ~= 1 or cap ~= "" then
  9.          table.insert(t,cap)
  10.           end
  11.           last_end = e+1
  12.           s, e, cap = str:find(fpat, last_end)
  13.        end
  14.        if last_end <= #str then
  15.           cap = str:sub(last_end)
  16.           table.insert(t, cap)
  17.        end
  18.     else
  19.         print("##ERROR failed to split ["..str.."] by:"..pat)
  20.     end
  21.     return t
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement