How to find last character of string?

Hi,

I have a simple-ish request today!
How would I find the last character of a string?

Thanks guys

Off Topic to the question, but the title has nothing to do with the post.

1 Like

OH SORRY!! I was replacing a previous draft haha

3 Likes

That would be string.sub(yourString, #yourString)

game:IsLoaded() do
	function getLastCharFromString(str)
		return string.sub(str,string.len(str)) or false
	end
	print(getLastCharFromString("your mom"))
end
5 Likes

Is there any reason that you’re adding a game:IsLoaded(), or why it’s in the do-end block, why is there an unnessesary or false which are never evaluated as strings are truthy in Lua(u) or that you’re using string.len over the # operator?


Alternatively you can use string.sub(yourString, -1), negative indexes starts from the end of the string to the beginning of the string so that -1 is the last index while -2 is the last 2 index, etc.

11 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.