Reputation: 395
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
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
Reputation: 122383
text = text:sub(1, -2)
The index -2
in string.sub
means the second character from the last.
Upvotes: 14