user3499641
user3499641

Reputation: 395

How to delete the last character of the text?

I have the following code:

text = "sometext"
print( string.sub(text, ( #text - 1 )) )

I want delete the last character in text.

Upvotes: 25

Views: 36768

Answers (2)

ORGL23
ORGL23

Reputation: 454

You can do like this:

text = "sometext" <-- our string
text = text:sub(1, -2)
print(text) <-- gives "sometex"

For ❤✱♔" this i did like this way

function deleteLastCharacter(str)
return(str:gsub("[%z\1-\127\194-\244][\128-\191]*$", ""))
end

for _, str in pairs{"❤✱♔" }do
print( deleteLastCharacter(str))
end

Upvotes: 37

Yu Hao
Yu Hao

Reputation: 122383

text = text:sub(1, -2)

The index -2 in string.sub means the second character from the last.

Upvotes: 14

Related Questions