SHARE
TWEET

[Lua]String Split

DerMarten Apr 23rd, 2013 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top