字符串中的最后一个字符索引

16 lua

如何获取Lua中字符串中字符的最后一个索引?

"/some/path/to/some/file.txt"
Run Code Online (Sandbox Code Playgroud)

如何获取/上述字符串中最后一个的索引?

Amb*_*ber 23

index = string.find(your_string, "/[^/]*$")
Run Code Online (Sandbox Code Playgroud)

(基本上,找到模式"正斜杠,然后零或多个不是正斜杠的东西,然后是字符串的结尾"的位置.)


Ego*_*off 7

这种方法更快一些(它从字符串的末尾搜索):

index = your_string:match'^.*()/'
Run Code Online (Sandbox Code Playgroud)