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  
  • 相关阅读:
    使用respondsToSelector:来发现对象是否响应消息
    使用iskindofclass来发现对象是否是某类或其子类的实例
    集合set的使用
    字典的使用
    数组的使用
    对NSNumber的理解
    数组、字典和集合的定义
    强引用strong和弱引用weak的定义
    类工厂方法的定义
    POJ 2262 / UVa 543
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/5594518.html
Copyright © 2011-2022 走看看