Skip to content
Related Articles
Open in App
Not now

Related Articles

How to remove a character from string in JavaScript ?

Improve Article
Save Article
  • Last Updated : 05 Jan, 2023
Improve Article
Save Article

In this article, we are given a string and the task is to remove a character from the given string. We have many methods to remove a character from a string that are described below.

Method 1: Using JavaScript replace() Method.

The replace method is used to replace a specific character/string with another character/string. It takes two parameters, the first is the string to be replaced and the second is the string that is to be replaced with. In this case, the first parameter is the character that is to be removed and the second parameter can be given as an empty string. This will remove the character from the string. This method removes the first occurrence of the string.

Syntax:

string.replace('characterToReplace', '');

Example: This example shows the above-explained approach

html




<h1 style="color: green">
    GeeksforGeeks
</h1>
 
<b>
    How to remove a character from
    a string using Javascript?
</b>
 
<p>Original string is GeeksforGeeks</p>
 
<p>
    New String is:
    <span class="output"></span>
</p>
 
<button onclick="removeCharacter()">
    Remove Character
</button>
 
<script type="text/javascript">
    function removeCharacter() {
        originalString = 'GeeksForGeeks';
        newString = originalString.replace('G', '');
     
        document.querySelector('.output').textContent
                = newString;
    }
</script>

Output:

Remove a character from string 

Method 2: Using JavaScript replace() Method with a Regular Expression.

This method is used to remove all occurrences of the specified character, unlike the previous method. A regular expression is used instead of the string along with the global property. It will select every occurrence in the string and it can be removed. 

Syntax:

string.replace(/regExp/g, '');

Example: This example shows the above-explained approach

html




<h1 style="color: green">
    GeeksforGeeks
</h1>
 
<b>
    How to remove a character from
    a string using Javascript?
</b>
 
<p>Original string is GeeksforGeeks</p>
 
<p>
    New String is:
    <span class="output"></span>
</p>
 
<button onclick="removeCharacter()">
    Remove Character
</button>
 
<script type="text/javascript">
    function removeCharacter() {
        originalString = 'GeeksForGeeks';
        newString = originalString.replace(/G/g, '');
     
        document.querySelector('.output').textContent
                = newString;
    }
</script>

Output:

Remove a character from string 

Method 3: Removing the first or last character using JavaScript slice() Method.

The slice() method is used to extract parts of a string between the given parameters. This method takes the starting index and the ending index of the string and returns the string in between these indices. If the ending index is not specified, it is assumed to be the length of the string. The first character could be removed by specifying the beginning index to be 1. It extracts the string from the second character up to the end of the string. The last character could be removed by specifying the ending index to be one less than the length of the string. This extracts the string from the beginning of the string to the second to last character.

Syntax:

// Removing the first character
string.slice(1);

// Removing the last character
string.slice(0, string.length - 1);

Example: This example shows the above-explained approach

html




<!DOCTYPE html>
<h1 style="color: green">
    GeeksforGeeks
</h1>
 
<b>
    How to remove a character from
    a string using Javascript?
</b>
 
<p>Original string is GeeksforGeeks</p>
 
<p>
    First character removed string:
    <span class="output1"></span>
</p>
 
<p>
    Last character removed string:
    <span class="output2"></span>
</p>
 
<button onclick="removeCharacter()">
    Remove Character
</button>
 
<script type="text/javascript">
    function removeCharacter() {
        originalString = 'GeeksForGeeks';
        firstCharRemoved = originalString.slice(1);
         
        lastCharRemoved =
            originalString.slice(0, originalString.length - 1);
     
        document.querySelector('.output1').textContent
                = firstCharRemoved;
        document.querySelector('.output2').textContent
                = lastCharRemoved;
    }
</script>

Output:

Remove a character from string

Remove a character from string 

Method 4: Removing a particular character at a given index using JavaScript substr() method.

This method can be used to remove a character from a particular index in the string. The substr() method is used to extract parts of a string between the given parameters. This method takes two parameters, one is the starting index and the other is the ending index of the string. The string between these indices is returned. The portion of the string before and after the character to be removed is separated and joined together. This removes the character from the specific index.

Syntax:

string.substr(0, position - 1) + string.substr(position, string.length);

Example: This example shows the above-explained approach

html




<h1 style="color: green">
    GeeksforGeeks
</h1>
 
<b>
    How to remove a character from
    a string using Javascript?
</b>
 
<p>Original string is GeeksforGeeks</p>
 
<p>
    New String is:
    <span class="output"></span>
</p>
 
<!-- Removing the 6th character -->
<button onclick="removeCharacter(6)">
    Remove Character
</button>
 
<script type="text/javascript">
    function removeCharacter(position) {
        originalString = 'GeeksForGeeks';
         
        newString = originalString.substr(0, position - 1)
        + originalString.substr(position, originalString.length);
     
        document.querySelector('.output').textContent = newString;
    }
</script>

Output:

Remove a character from string

Remove a character from string 

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!