DEV Community

Cover image for Remove last character from javascript string
biplab3
biplab3

Posted on

Remove last character from javascript string

There are two methods given below by which you can remove last character from javascript string.

slice():- This method is used to cut a part of the string and return that part as a new string. This method takes two parameters as the start position and the end position. In javascript, the position starts from zero.

Syntax:-

slice(start position index, end position index)

Also read, Javascript String Methods with example

Below is an example to remove last character from javascript string

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
</head>
<body>
   <h3>Remove the last cahracter from a string in Javscript</h3>
   <input type="text" id="rmv">
   <button type="button" onclick="getString()">Remove</button>
   <p><b>Result:</b></p>
   <p id="res"></p>
   <script type="text/javascript">
    function getString(){
        var string = document.getElementById('rmv').value;
        alert(string);
        let new_string = string.slice(0,-1);
        document.getElementById('res').innerHTML = new_string;
    }
   </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output:-

Image description

Explanation of the code:-

In the above example, you have noticed that zero is taken as the first parameter inside the slice() function, and -1 is taken as the second parameter because index position -1 is considered as the last character.

So, you must know that the slice() function returns the extracted string except for the last character. That is why the slice() function helps us to remove the last character.

substr():- This function also extracts a part of the string and returns the extracted part as the new string depending on its arguments and it takes two arguments. The first argument is the index number of the first position i.e. start position and the second argument is the total length of the string.

The substr() is similar to the slice() function but the only difference is, it takes the second argument as the length of the string instead of the end position index.

Syntax:-

substr(start position index, length)

Below is an example of substr() function:-

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
</head>
<body>
   <h3>Remove the last cahracter from a string in Javscript</h3>
   <input type="text" id="rmv">
   <button type="button" onclick="getString()">Remove</button>
   <p><b>Result:</b></p>
   <p id="res"></p>
   <script type="text/javascript">
    function getString(){
        var string = document.getElementById('rmv').value;
        let new_string = string.substr(0,string.length-1);
        document.getElementById('res').innerHTML = new_string;
    }
   </script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Output:-

Image description

Explanation of the code:-

In the above example, you have noticed that I have taken the first argument as the index number of the first position which is 0 and the second argument is a string.length-1. For example, if a string contains a length of 5 then string.length-1 gives us 4 as the string length which means the last character is ignored. Finally, we have removed the last character.

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below

Top comments (0)