源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Lua中实现php的strpos()以及strrpos()函数

  • 时间:2021-11-29 23:21 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Lua中实现php的strpos()以及strrpos()函数
在来写一个lua中实现php的strpos()函数,查找某个字符串在指定字符串首次出现的位置,其实lua中也为我们提供了这样的函数使用string.find()即可获得,下面我们还是简单写一个函数,代码如下:
[u]复制代码[/u] 代码如下:
function strpos (str, f)       if str ~= nil and f ~= nil then           return (string.find(str, f))       else          return nil       end   end 
测试如下图所示: [img]http://files.jb51.net/file_images/article/201411/2014113153610899.png?2014103153626[/img] 下面在来个strrpos()函数,查找某个字符串在指定字符串最后一次出现的位置,下面我们还是简单写一下函数,代码如下:
[u]复制代码[/u] 代码如下:
function strrpos (str, f)       if str ~= nil and f ~= nil then           local t = true          local offset = 1          local result = nil           while (t)           do              local tmp = string.find(str, f, offset)               if tmp ~= nil then                   offset = offset + 1                  result = tmp               else                  t = false              end           end           return result       else          return nil       end   end 
测试如下图(注意:如果要查找 . 需要进行转义,使用"%."): [img]http://files.jb51.net/file_images/article/201411/2014113153744723.png?201410315386[/img] 好了,今天就先到这里,以后我们继续实现其他函数功能
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部