How to Remove Last Character From String Using jQuery

Last Updated on December 18, 2020 by Roshan Parihar

In this tutorial, learn how to remove last character from the string using jQuery. The short answer is to use substring() to delete the last letter from the string.

To delete the last letter from the string, you can also use the jQuery slice(). You have to use the methods given in the examples here.

Let’s find out these methods below.

Remove Last Character From String Using jQuery Substring()

To delete the end letter of the string, you have to use jQuery substring().

Firstly, you have to get the text content in a variable using the jQuery text(). After that, use the jQuery substring() with argument 0, myTextSub.length-1 in which ‘myTextSub’ is the variable that stores the string using text(). See the example below to get the idea of using the method.

Test it Live

The above example contains the ‘&’ symbol at the end of the string that you have to remove. Click the above button to see the live output.

Delete the Last Letter with jQuery Slice()

In addition to the above method, you can also use the jQuery slice() to delete the last letter. This method works same as substring() to removes the last letter of the string. Firstly, you have to get the text content using text(). After that, use the jQuery slice() with an argument 0, -1 to delete the last letter.

Test it Live

Now, you can check the live demo with the above button to check the example delete the last letter of the string. You will get the same string as you got in the above jQuery substring() method in the previous example.

You may also like to read.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.