I have the following code:
text = "sometext"
print( string.sub(text, ( #text - 1 )) )
I want delete the last character in text
.
The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.
Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
To remove the first and last characters from a string, call the slice() method, passing it 1 and -1 as parameters, e.g. str. slice(1, -1) . The slice method returns a new string containing the extracted section from the original string.
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
text = text:sub(1, -2)
The index -2
in string.sub
means the second character from the last.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With