Saturday, September 21, 2024
Google search engine
HomeLanguagesHow to remove a character from string in JavaScript ?

How to 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.

Methods to Remove a Character from String:

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

Javascript




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


Output

eeksForGeeks

Output:

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

Javascript




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


Output

eeksForeeks

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

Javascript




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: 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

Javascript




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


Output

GeeksorGeeks

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.

RELATED ARTICLES

Most Popular

Recent Comments