JavaScript Remove First and Last Character From String

To remove first, last and specific character from string javascript; Through this tutorial, i am going to show you how to remove first, last and specific character from the string in javaScript.

JavaScript Remove First, Last and Specific Character From String

  • Use the substr() function to remove the first character from a string in JavaScript.
  • Use the substring() function to remove the last character from a string in JavaScript.
  • Use the replace() function to remove the specific character from a string in JavaScript.

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

Here, i will take an example for how to remove the first character from string in javascript using substr() function; as shown below:

<script type="text/javascript">
    var myString = '!Welcome to Laratutorials.com';
    var sillyString = myString.substr(1);
    document.write('Result of the above code is:-' + sillyString);
</script>

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

Here, i will take second example using substring() function for how to remove the last character from a string in javascript; as shown below:

<script type="text/javascript">
     var str= "Welcome To Tutsmake.com!";
     var newStr = str.substring(0, str.length - 1);
     document.write('Result of the above code is:-' + newStr);
</script>

Use the replace() function to remove the specific character from a string in JavaScript.

Here, i will take thired example using replace() function for how to remove a specific character from string javascript; as shown below:

 var string = 'hello javascript'; // just an example
 var test = string.replace(/^hello+/i, ''); //'javascript'
 document.write('Result of the above code is:-' + test);

Conclusion

javascript remove first, last and specific character from string; In this tutorial, you have used to substring and substr function to remove the first, last and specific character from string in javaScript.

More JavaScript Tutorials

Be First to Comment

Leave a Reply

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