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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html lang="en"> <head> <title>jQuery Remove Last Character From String Using Substring()</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function(){ $('#btnRemoveFirstSub').click(function(){ var myTextSub = $('.MyStringSub').text(); var RemoveFirstCharSub = myTextSub.substring(0, myTextSub.length-1); $('.removedStringSub').html('<strong>The new string is: </strong>'+RemoveFirstCharSub); }); }); </script> </head> <body> <h2>jQuery Remove Last Character From String Using Substring()</h2> <p class="MyStringSub">This is my string&</p> <button type="button" id="btnRemoveFirstSub">Click me to Remove First Character</button> <p class="removedStringSub"></p> </body> </html> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html lang="en"> <head> <title>jQuery Delete Last Character From String Using slice()</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function(){ $('#btnRemoveFirstSlice').click(function(){ var myTextSlice = $('.MyStringSlice').text(); var RemoveFirstCharSlice = myTextSlice.slice(0, -1); $('.removedStringSlice').html('<strong>The new string is: </strong>'+RemoveFirstCharSlice); }); }); </script> </head> <body> <h2>jQuery Delete Last Character From String Using slice()</h2> <p class="MyStringSlice">This is my string@</p> <button type="button" id="btnRemoveFirstSlice" class="btn btn-primary">Delete First Character</button> <p class="removedStringSlice"></p> </body> </html> |
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.