现在的位置: 首页Lua>正文
Lua中实现php的strrpos()函数 [原创]
2012年08月24日 Lua 评论数 2 ⁄ 被围观 14,116 次+

刚才写一个lua上实现php中的strpos()函数,下面在来个strrpos()函数,查找某个字符串在指定字符串最后一次出现的位置,下面我们还是简单写一下函数,代码如下:

  1. function strrpos (str, f)   
  2.     if str ~= nil and f ~= nil then   
  3.         local t = true  
  4.         local offset = 1  
  5.         local result = nil   
  6.         while (t)   
  7.         do  
  8.             local tmp = string.find(str, f, offset)   
  9.             if tmp ~= nil then   
  10.                 offset = offset + 1  
  11.                 result = tmp   
  12.             else  
  13.                 t = false  
  14.             end   
  15.         end   
  16.         return result   
  17.     else  
  18.         return nil   
  19.     end   
  20. end  

测试如下图(注意:如果要查找 . 需要进行转义,使用"%."):

本文地址:http://www.92csz.com/06/1190.html
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog
 

目前有 2 条留言 其中:访客:1 条, 博主:1 条

  1. zxl : 2013年06月22日21:39:04  -49楼

    第10行: offset = offset + 1
    修改成: offset = tmp + 1
    效率会更高

    • clairelume : 2013年06月24日11:23:21

      多谢指正! 😛