Advertisement
nelsonlombardo

String split

Jul 17th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.49 KB | None | 0 0
  1. -- Separa un string según el patrón que le pasemos
  2. function split(str, pat)
  3.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  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.    return t
  19. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement