Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I remove the last character off a string?

Asked by
ItsMeKlc 235 Moderation Voter
8 years ago

How can I remove the last character off a string?

1 answer

Log in to vote
0
Answered by
Unclear 1776 Moderation Voter
8 years ago

Use the sub method and the # operator.

The sub method takes the following arguments like so, assuming string points to a string

local substring = string:sub(startIndex, endIndex)

where startIndex is the nth character you want the new substring to start at and endIndex is the nth character you want the new substring to end at.

So, for our case, we want startIndex to be 1 and endIndex to be 1 less than the length of the string. We can get the length of the string using the # operator.

local substring = string:sub(1, #string - 1)
0
I tried stringe= "5555gg" local substring = stringe:sub(1, #string - 1) print(substring), but it only seems to print the first two characters... ItsMeKlc 235 — 8y
Ad

Answer this question