Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the last character of the text?

Tags:

string

lua

I have the following code:

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

I want delete the last character in text.

like image 596
user3499641 Avatar asked Jul 17 '14 08:07

user3499641


People also ask

How do I remove the last character?

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.

How do I delete a specific 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.

How do you delete first and last characters?

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.


2 Answers

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
like image 145
ORGL23 Avatar answered Oct 21 '22 23:10

ORGL23


text = text:sub(1, -2)

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

like image 14
Yu Hao Avatar answered Oct 21 '22 22:10

Yu Hao