Guest User

Untitled

a guest
Apr 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.76 KB | None | 0 0
  1. -------------------------------------------------
  2. function string.split(s, pattern, maxsplit)
  3.  
  4.     if type(s) ~= "string" or type(pattern) ~= "string" or type(maxsplit) ~= "string" then
  5.  
  6.         return nil
  7.  
  8.     end
  9.  
  10.     if pattern == nil then
  11.  
  12.         pattern = " "
  13.  
  14.     end
  15.  
  16.     if maxsplit == nil then
  17.  
  18.         maxsplit = -1
  19.  
  20.     end
  21.  
  22.     if string.find(s, pattern) == nil then
  23.  
  24.         return {s}
  25.  
  26.     end
  27.  
  28.     local strings = {}
  29.     local index = 1
  30.     local last_index = 1
  31.  
  32.     while true do
  33.  
  34.         index = string.find(s, pattern, index)
  35.  
  36.         if index == nil or #strings == maxsplit then
  37.  
  38.             table.insert(strings, string.sub(s, last_index))
  39.  
  40.             return strings
  41.  
  42.         end
  43.  
  44.         table.insert(strings, string.sub(s, last_index, index - 1))
  45.         index = index + #pattern
  46.         last_index = index
  47.  
  48.     end
  49.  
  50. end
Add Comment
Please, Sign In to add comment