Advertisement
nelsonlombardo

String Split

Jul 17th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Separa una cadena (str) según el patrón (pat) que le
  2. -- indiquemos.
  3. function split(str, pat)
  4.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  5.    local fpat = "(.-)" .. pat
  6.    local last_end = 1
  7.    local s, e, cap = str:find(fpat, 1)
  8.    while s do
  9.       if s ~= 1 or cap ~= "" then
  10.      table.insert(t,cap)
  11.       end
  12.       last_end = e+1
  13.       s, e, cap = str:find(fpat, last_end)
  14.    end
  15.    if last_end <= #str then
  16.       cap = str:sub(last_end)
  17.       table.insert(t, cap)
  18.    end
  19.    return t
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement