10

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
4
  • 3
    What problem are you trying to solve exactly?
    – lhf
    Dec 8, 2013 at 23:09
  • 2
    I think simplest way is just use string.find ('my.string.here.'):find("%.[^.]-$"). but you need escape special chars (e.g. ., *, + etc.). Also in Lua result should be 15.
    – moteus
    Dec 9, 2013 at 8:36
  • @moteus nice suggestion, but I wanted common solution (finding index for numbers as well, for example)
    – Kosmetika
    Dec 9, 2013 at 9:25
  • @lhf Why do you even ask? First and last index of string in substring are widely used functions accross all programming languages! There's no need for him to explain any specific problem... Mar 5, 2016 at 19:57

5 Answers 5

10
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.

4

If you have performance concerns, then this might be a bit faster if you're using Luvit which uses LuaJIT.

local find = string.find
local function lastIndexOf(haystack, needle)
    local i, j
    local k = 0
    repeat
        i = j
        j, k = find(haystack, needle, k + 1, true)
    until j == nil

    return i
end

local s = 'my.string.here.'
print(lastIndexOf(s, '.')) -- This will be 15.

Keep in mind that Lua strings begin at 1 instead of 0 as in JavaScript.

2
  • found this interesting post regarding performance - neil.fraser.name/news/2009/12/25, just curiosity did this answer copes with performance - stackoverflow.com/a/20460403/2117550 ?
    – Kosmetika
    Dec 9, 2013 at 9:23
  • 1
    @Kosmetika Interesting indeed. I hadn't seen it before, but what I wrote is very close to lastIndexOfFind. The only big difference is that my function returns nil as opposed to -1, which is more Lua style. Oh, and my function picks up from the second return value of string.find, which means it skips some unnecessary substrings and is theoretically faster. I believe @lhf's answer is more performant on plain Lua and less so on LuaJIT 2.1 due to string.match not being compiled yet but fixed pattern string.find is, but you'll have to perform your own tests for your use case.
    – Ryan Stein
    Dec 9, 2013 at 15:24
4

To search for the last instance of string needle in haystack:

function findLast(haystack, needle)
    --Set the third arg to false to allow pattern matching
    local found = haystack:reverse():find(needle:reverse(), nil, true)
    if found then
        return haystack:len() - needle:len() - found + 2 
    else
        return found
    end
end

print(findLast("my.string.here.", ".")) -- 15, because Lua strings are 1-indexed
print(findLast("my.string.here.", "here")) -- 11
print(findLast("my.string.here.", "there")) -- nil

If you want to search for the last instance of a pattern instead, change the last argument to find to false (or remove it).

3
  • 2
    Your example returns 1 for me.
    – Ryan Stein
    Dec 8, 2013 at 22:35
  • Sorry, from memory - I must have forgotten how Lua strings work.
    – joews
    Dec 8, 2013 at 22:38
  • I've tested it also with empty strings. Works flawlessly. I like this more than other solutions because it's simple and clever.
    – Wes
    Mar 27 at 21:02
3

Here’s a solution using LPeg’s position capture.

local lpeg      = require "lpeg"
local Cp, P     = lpeg.Cp, lpeg.P
local lpegmatch = lpeg.match

local cache = { }

local find_last = function (str, substr)
  if not (str and substr)
    or str == "" or substr == ""
  then
    return nil
  end
  local pat = cache [substr]
  if not pat then
    local p_substr   = P (substr)
    local last       = Cp() * p_substr * Cp() * (1 - p_substr)^0 * -1
    pat = (1 - last)^0 * last
    cache [substr] = pat
  end
  return lpegmatch (pat, str)
end

find_last() finds the last occurence of substr in the string str, where substr can be a string of any length. The first return value is the position of the first character of substr in str, the second return value is the position of the first character following substr (i.e. it equals the length of the match plus the first return value).

Usage:

local tests = {
  A    = [[fooA]],                      --> 4, 5
  [""] = [[foo]],                       --> nil
  FOO  = [[]],                          --> nil
  K    = [[foo]],                       --> nil
  X    = [[X foo X bar X baz]],         --> 13, 14
  XX   = [[foo XX X XY bar XX baz X]],  --> 17, 19
  Y    = [[YYYYYYYYYYYYYYYYYY]],        --> 18, 19
  ZZZ  = [[ZZZZZZZZZZZZZZZZZZ]],        --> 14, 17
  --- Accepts patterns as well!
  [P"X" * lpeg.R"09"^1] = [[fooX42barXxbazX]], --> 4, 7
}

for substr, str in next, tests do
  print (">>", substr, str, "->", find_last (str, substr))
end
0

Can be optimized but simple and does the work.

function lastIndexOf(haystack, needle)
  local last_index = 0
  while haystack:sub(last_index+1, haystack:len()):find(needle) ~= nil do
    last_index = last_index + haystack:sub(last_index+1, haystack:len()):find(needle)
  end
  return last_index
end

local s = 'my.string.here.'
print(lastIndexOf(s, '%.')) -- 15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.