How to Gsub the last Char of a string?

so I am trying to do something like this:

local str = " @ @"

str = string.gsub(str, string.sub(str, -1, -1), "Yes") -- > "@ Yes"

However, instead of only doing the last letter, which is an @ symbol, it replaces both with yes, this is because it is saying the last character is an @ based on the position given with string.sub. So what I’m essentially looking for is a way to tell it to replace ONLY the LAST character of the string, and no other instances of that character other than the last one.

Well, if you’re not dead set on gsub you can do something like this:

local str = "@ @"
str = str:sub(1, -2)  -- Removes the last character in a string
str = str .. "Yes" -- Add yes to the end of the string
print(str) --> "@ Yes" 

yeah my friend just helped me out, he said

string.gsub(input.msg.Text, ".$", i, 1)

which worked perfectly. I will mark yours as solution however as it works as well. thank you!

it took us like an hour to figure this out and i posted here cause i gave up at that point haha.

Ahh, your friend clutched up haha. No problem, happy to help.

Yeah, facts lol, I don’t really use string patterns all that often so it was kind of a guess and check type of thing and I couldn’t find any related posts.