Advertisement
DerMarten

[Lua]String Split

Apr 23rd, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.69 KB | None | 0 0
  1. -- Beispiel/example
  2. -- local test = string.split(string , separator)
  3. -- local vert1 = test[1]
  4. -- local vert2 = test[2]
  5. -- ...
  6. function string:split(sSeparator, nMax, bRegexp)
  7.     assert(sSeparator ~= '')
  8.     assert(nMax == nil or nMax >= 1)
  9.  
  10.     local aRecord = {}
  11.  
  12.     if self:len() > 0 then
  13.         local bPlain = not bRegexp
  14.         nMax = nMax or -1
  15.  
  16.         local nField=1 nStart=1
  17.         local nFirst,nLast = self:find(sSeparator, nStart, bPlain)
  18.         while nFirst and nMax ~= 0 do
  19.             aRecord[nField] = self:sub(nStart, nFirst-1)
  20.             nField = nField+1
  21.             nStart = nLast+1
  22.             nFirst,nLast = self:find(sSeparator, nStart, bPlain)
  23.             nMax = nMax-1
  24.         end
  25.         aRecord[nField] = self:sub(nStart)
  26.     end
  27.  
  28.     return aRecord
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement