Open In App

Remove a Character From String in JavaScript

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 is described below.

Method 1: Using JavaScript replace() Method

The replace() method replaces the first occurrence of a specified character/string with another character/string.

Syntax:

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

Example: This example shows the above-explained approach

function removeCharacter() {
    let originalString = "GeeksForGeeks";
    newString = originalString.replace("G", "");
    console.log(newString);
}

removeCharacter();

Output
eeksForGeeks

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

This method is used to remove all occurrences of a specified character or string from the input string, unlike the previous method, which removes only the first occurrence.

It uses a regular expression with the global property to select and remove every occurrence.

Syntax:

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

Example: This example shows the above-explained approach

function removeCharacter() {
    originalString = "GeeksForGeeks";
    newString = originalString.replace(/G/g, "");
    console.log(newString);
}

removeCharacter();

Output
eeksForeeks

Method 3: Using JavaScript slice() Method

The slice() method is used to extract parts of a string between specified indices.

Syntax:

string.slice(start, end)

Example: This example shows the above-explained approach

function removeCharacter() {
    originalString = "GeeksForGeeks";
    firstCharRemoved = originalString.slice(1);

    lastCharRemoved = originalString
            .slice(0, originalString.length - 1);

    console.log(firstCharRemoved);
    console.log(lastCharRemoved);
}

removeCharacter();

Output
eeksForGeeks
GeeksForGeek

Method 4: Using JavaScript substr() method

The substr() method is used to remove a character from a specific index within the string.

It extracts portions of a string between specified parameters and then joins the parts before and after the character to be removed.

Syntax:

string.substr(start, length)

Example: This example shows the above-explained approach

function removeCharacter(position) {
    originalString = "GeeksForGeeks";
    newString =
        originalString.substr(0, position - 1)+
        originalString.substr(
            position,
            originalString.length
        );
    console.log(newString);
}

removeCharacter(6);

Output
GeeksorGeeks

Method 5: Using JavaScript split() and join() methods

The split() method is used to split a string into an array of substrings based on a specified separator. By splitting the string at the character to be removed, and then joining the array elements back into a string, the desired character can be effectively removed.

Syntax:

string.split('characterToSplitAt').join('');

Example: This example demonstrates the above-explained approach:

function removeCharacter() {
    let originalString = "GeeksForGeeks";
    let newString = originalString.split("G").join("");
    console.log(newString);
}
 
removeCharacter();

Output
eeksForeeks

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.

Article Tags :