Need help with strings!

I’m trying to create code that’ll be able to get the last few characters of a string, like the 2nd or 3rd to last character of a string. I already have code that’ll get the very last character of a string, i’m just curious on how I can edit the code so I can get the 2nd or 3rd to last character of a string if need be.

Code:

string.sub("placeholder text",string.len("placeholder text"))

All help is appreciated!

Basically how string.sub works is:

string.sub(String, Pos1, Pos2)
string.sub will return all text between Pos1 and Pos1 with the given String

You can’t actually edit a string but you can make 2 strings and combine them to get the same effect, example for what you want:

local String = "Hey whats up hello"

local End = 3 --How many characters we're gonna slice off the end
local CutOff = string.sub(String, (string.len(String) - End), string.len(String)) --Incase you want to get what you're cutting off this will grab it for you
local FirstHalf = string.sub(String, 1, (string.len(String) - End)) --Grabs the first half of the string except for the part we want cut off
local SecondHalf = "llo" --This would be the string we replace the cut off part with
local FinalString = FirstHalf..SecondHalf --This will combine the strings

Hope this helped if you have any further questions just ask :smiley:

1 Like