Find the last index of a character in a string

lua, string

Solution

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.

Problem

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. In Javascript it looks like: ``` 'my.string.here.'.lastIndexOf('.') // returns 14 ```

Original source