查找字符串中字符的最后一个索引 [英] Find the last index of a character in a string

查看:606
本文介绍了查找字符串中字符的最后一个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够对Lua(Luvit)项目中的字符串使用lastIndexOf方法.不幸的是,没有内置这种方法,我现在有点卡住了.

I want to have ability to use a lastIndexOf method for the strings in my Lua (Luvit) project. Unfortunately there's no such method built-in and I'm bit stuck now.

在Javascript中,它看起来像:

In Javascript it looks like:

'my.string.here.'.lastIndexOf('.')     // returns 14

推荐答案

function findLast(haystack, needle)
    local i=haystack:match(".*"..needle.."()")
    if i==nil then return nil else return i-1 end
end
s='my.string.here.'
print(findLast(s,"%."))
print(findLast(s,"e"))

请注意,要找到.,您需要对其进行转义.

Note that to find . you need to escape it.

这篇关于查找字符串中字符的最后一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆