zoukankan      html  css  js  c++  java
  • 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  
  • 相关阅读:
    V2热帖:要多健壮的代码才能支撑起千变万化的需求?
    jmeter生成html报告的命令
    jmeter5.x&4.x搭配使用Serveragent 监听服务端性能参数
    springboot关于tomcat的几个默认配置
    nginx日志统计分析-shell
    OpenStack虚拟机VIP配置步骤
    openstack 3.14.3 虚拟机增加指定IP网卡
    OpenStack各组件的常用命令
    Filebeat的Registry文件解读
    一个shell脚本的实践
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/5594518.html
Copyright © 2011-2022 走看看