How to Remove Last & First Character from String in JavaScript

How to Remove Last & First Character from String in JavaScript

Overview

In this tutorial – we will explain with an example, how to remove first & the last character from string in javaScript?

Removing a string from a character is a common problem you encounter when developing a software application. In this tutorial, you will learn about various ways to remove a character from a string using JavaScript function.

Question:- How to Remove the first character from string in javaScript?

Answer:- Use the substr() function to remove the first character from a string in JavaScript.

The following example will show you how to remove the first character from string in javascript.

<script type="text/javascript">
    $(document).ready(function(){
        $(".remove_first").click(function(){
            var myString = '!Welcome to Tutsmake.com';
            var sillyString = myString.substr(1);
            alert(sillyString);
        });
    });
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remove first character from string javaScript</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>   
<script type="text/javascript">
    $(document).ready(function(){
        $(".remove_first").click(function(){
            var myString = '!Welcome to Tutsmake.com';
            var sillyString = myString.substr(1);
            alert(sillyString);
        });
    });
</script>
</head>
<body>
    <button type="button" class="remove_first">Click and Remove First Character From String</button>
</body>
</html>

Question:- How to Remove the last character from string in javaScript?

Answer:- Use the substring() function to remove the last character from a string in JavaScript.

The following example will show you how to remove the last character from a string in javascript.

<script type="text/javascript">
    $(document).ready(function(){
        $(".remove_last").click(function(){
            var str= "Welcome To Tutsmake.com!";
            var newStr = str.substring(0, str.length - 1);
            alert(newStr);
        });
    });
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remove last character from string javaScript</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>   
<script type="text/javascript">
    $(document).ready(function(){
        $(".remove_last").click(function(){
            var str= "Welcome To Tutsmake.com!";
            var newStr = str.substring(0, str.length - 1);
            alert(newStr);
        });
    });
</script>
</head>
<body>
   <button type="button" class="remove_last">Click and Remove Last Character From String</button>
</body>
</html>

Conclusion

In this tutorial, we have used to substring and substr function to remove the first & the last character from string in javaScript.

Leave a Reply

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