Hi,
I need to use string.find, that it will find from end to start, how can i do it? (so it will return the las result)
I am unsure of what you mean, you want the text to be reversed?
I need, that when i use find, it will return position of the last match.
There’s a lot of information missing here. By last result do you mean the last char in a string, or the last string in an array?
string:find() finds a string within a string, for example
local string_here = "This is cool"
if string_here:find("Cool") then
print("Cool Found")
end
You can’t exactly find it “from end to start”
So i cant get position of the last match.
You want to return the last word in a string? or the position of that word?
I mean the same as string.find does, but not from start, but from end.
Nope,
when I do string.find(“123”) it will retur start and end of 1st 123 in string, but i need to get pos of last.
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.
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 …
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)
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
That won’t work, you need to change string to v.
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?