JavaScript Remove First/Last/Specific Character From String

JavaScript Remove First/Last/Specific Character From String

Overview

In this tutorial – we will explain with an example, how to remove first, last character and specific character from the 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">
    var myString = '!Welcome to Tutsmake.com';
    var sillyString = myString.substr(1);
    document.write('Result of the above code is:-' + sillyString);
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remove first character from string javaScript</title>
<script type="text/javascript">

    var myString = '!Welcome to Tutsmake.com';
    var sillyString = myString.substr(1);
    document.write('Result of the above code is:-' + sillyString);

</script>
</head>
<body>

</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">
     var str= "Welcome To Tutsmake.com!";
     var newStr = str.substring(0, str.length - 1);
     document.write('Result of the above code is:-' + newStr);
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remove last character from string javaScript</title> 
<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>
</head>
<body>

</body>
</html>

Question:- How to remove specific character from string javascript?

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

The following example will show you how to remove a specific character from string javascript.

 var string = 'hello javascript'; // just an example
 var test = string.replace(/^hello+/i, ''); //'javascript'
 document.write('Result of the above code is:-' + test);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript remove specific character from string</title>
</head>
<body>
<script>

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

  </script>  
</body>
</html>

Conclusion

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

You may like

  1. Functions: RemoveFirst Character From String PHP
  2. PHP Functions : Remove Last Character From String In PHP
  3. Remove Specific/Special Characters From String In PHP
  4. How to Replace First and Last Character From String PHP

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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