How to find from end of string?

You could create a variable and make it a reverse of the string, then use the find method in reverse so looking for 123 would be 321 or make a custom find method that flips it for you.

1 Like

Ohhh so for example, you mean:

You have a string:
string = “Blue Red Red Blue”

And you want to get the last word “Blue”? (“Blue Red Red Blue”)

I’m sorry if this isn’t what you’re looking for, i’m having a hard time understanding.

Yes, exactly :smile:

Using @InternallyAmplified’s example, you can use string.split to get the last word.

local String = "Blue Red Red Blue"

local Words = string.split(String," ")

local LastWord = Words[#Words]

print(LastWord)
2 Likes

No, i dont want last blue, but last word, that i am finding, for example, if i am finding blue “blue red blue red”

Further adding to @Proxus’s Example, once you have lastword, you can iterate through them and pick out the position (in the newly made array)

local String = "Blue Red Red Blue"

function FindLastWordPosInArray(String, TargetWord)

local Words = string.split(String," ")

local lastwordpos = 0

local LastWord = Words[#Words]

for i, v in pairs(Words) do
if v == TargetWord then
lastwordpos = i
end
end
end
1 Like

That won’t work, you need to change string to v.

1 Like

But idk if it will be splited by " ", in this case, i see @kamsdogs solution as best.

Is it possible to make it so the words are split by a " ", Can you show us your code so we can optimize it to do that?

I need to make it universal, bec I am executing it on users input

What exactly are you trying to achieve?

That when i have got string and pattern, it will do the same as string.find, but the last result.

No, i know that, but you’re only executing it on userinputservice,

The string must come from elsewhere, so you should be able to split each word with a " ",

Right now are you trying to make a textbox with answers or something?

I apologize if this doesn’t exist in Lua as my primary languages are more C based, but you may be interested in looking into .rfind(). I believe it has a similar usage as to what you want.

1 Like

I am making text box for processor with easy program language, but i have problem with multiply logic operations, so i need to find the last keyword in string.

no, it doesnt exist, but i can do find(string.reverse(),string.reverse()) as you saied

Like my previous comment, I am unsure if Lua specifically has it but the exact thing I would use in this scenario in Java would be

str.lastIndexOf(userInput)

Use patterns. The “$” symbol will force matching at the end.

local s = "Hello ABC Hello"
print(s:find("Hello$"))

A popular use-case outside of Roblox is to check for file extensions:

local filename = "test.lua"
if (filename:find("%.lua$")) then
   -- Lua file
end

Check out the String Patterns article.

9 Likes

Ok, but idk if it will be on end, i just need to get the last match

Based on @kamsdogs idea, i made this

local str = "123789123 "
local ts,te = string.find(string.reverse(str),string.reverse("123"))
local s,e = string.len(str)-te+1,string.len(str)-ts+1
4 Likes