Lua自己实现string.split功能

 
  1. local function split(str, d) --str是需要查分的对象 d是分界符  
  2.     local lst = { }  
  3.     local n = string.len(str)--长度  
  4.     local start = 1  
  5.     while start <= n do  
  6.         local i = string.find(str, d, start) -- find 'next' 0  
  7.         if i == nil then   
  8.             table.insert(lst, string.sub(str, start, n))  
  9.             break   
  10.         end  
  11.         table.insert(lst, string.sub(str, start, i-1))  
  12.         if i == n then  
  13.             table.insert(lst, "")  
  14.             break  
  15.         end  
  16.         start = i + 1  
  17.     end  
  18.     return lst  
  19. end  


另一种:用指定字符或字符串分割输入字符串,返回包含分割结果的数组:

 from: http://blog.csdn.net/heyuchang666/article/details/51700017

 
    1. function string.split(input, delimiter)  
    2.     input = tostring(input)  
    3.     delimiter = tostring(delimiter)  
    4.     if (delimiter=='') then return false end  
    5.     local pos,arr = 0, {}  
    6.     -- for each divider found  
    7.     for st,sp in function() return string.find(input, delimiter, pos, true) end do  
    8.         table.insert(arr, string.sub(input, pos, st - 1))  
    9.         pos = sp + 1  
    10.     end  
    11.     table.insert(arr, string.sub(input, pos))  
    12.     return arr  
    13. end  
原文地址:https://www.cnblogs.com/GarfieldEr007/p/5594518.html