Remove a Character From String in JavaScript
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.
Table of Content
- Method 1: Using JavaScript replace() Method
- Method 2: Using JavaScript replace() Method with a Regular Expression
- Method 3: Using JavaScript slice() Method
- Method 4: Using JavaScript substr() method
- Method 5: Using JavaScript split() and join() methods
- Method 6: Using JavaScript splice() Method
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 = "w3wiki";
newString = originalString.replace("G", "");
console.log(newString);
}
removeCharacter();
Output
eeksForBeginner
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 = "w3wiki";
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.
- When removing the first character, you specify the starting index as 1, which extracts the string from the second character to the end.
- To remove the last character, you specify the ending index as one less than the string’s length, extracting the string from the beginning to the second-to-last character.
Syntax:
string.slice(start, end)
Example: This example shows the above-explained approach
function removeCharacter() {
originalString = "w3wiki";
firstCharRemoved = originalString.slice(1);
lastCharRemoved = originalString
.slice(0, originalString.length - 1);
console.log(firstCharRemoved);
console.log(lastCharRemoved);
}
removeCharacter();
Output
eeksForBeginner BeginnerForGeek
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 = "w3wiki";
newString =
originalString.substr(0, position - 1)+
originalString.substr(
position,
originalString.length
);
console.log(newString);
}
removeCharacter(6);
Output
BeginnerorBeginner
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 = "w3wiki";
let newString = originalString.split("G").join("");
console.log(newString);
}
removeCharacter();
Output
eeksForeeks
Method 6: Using JavaScript splice() Method
The splice() method changes the contents of a string by removing or replacing existing elements and/or adding new elements. We can use this method to remove a character at a specified index from the string.
Example:
function removeCharacter() {
let originalString = "w3wiki";
// Removing the first character
let firstCharRemoved = originalString.split('');
firstCharRemoved.splice(0, 1);
firstCharRemoved = firstCharRemoved.join('');
// Removing the last character
let lastCharRemoved = originalString.split('');
lastCharRemoved.splice(-1, 1);
lastCharRemoved = lastCharRemoved.join('');
console.log(firstCharRemoved);
console.log(lastCharRemoved);
}
removeCharacter();
Output
eeksForBeginner BeginnerForGeek
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.