How to remove last character in string

Say I have a string called “hello worldo”, how would I remove the last “o” without removing the “o” in hello? Should I create a character table and remove the last element or is there an easier way?

According to the string manipulation, the special character $ matches the last character. You can use string.match() with that and subtract it… or you can use string.sub(string, -1, 1).

So you are saying “$” = last character of any string?

I think so, you should try running:

local str = "test string"
print(string.match(str, "$"))

I tried and it prints nothing.

If that doesn’t work, try the latter method.

1 Like

You could just do:

str = str:Sub(1, #str - 1)

Im not entirely sure with this works, but it should.

1 Like
local str = "Hello worldo"
str = str:sub(0, -2)

Should remove the last character from str.

21 Likes

You can use string.gsub for that.
If you don’t know how it works, read this:

It will help you.

str = "hello worldo";

str = str:gsub(".?$","");

Basically, what that does is it replaces “o” (one (?) last ($) any character (.)) with nothing.
Hope I helped.

20 Likes