• 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  
  • 相关阅读:
    java 安全沙箱模型详解
    ProcessLifecycleOwner判断Android应用程序前后台切换
    React 使用 antd 实现按需加载
    数据湖!这是个什么东东!!!
    Linux sed awk 总结之001
    Linux超实用网络篇汇总001
    非常实用Curl命令整理
    Mysql 查找锁定事务并kill
    sqlserver 查看sql语句是否被重用
    Linux下配置mysql允许指定IP远程访问
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/5594518.html
Copyright © 2020-2023  润新知