How to usethe split() and join() methods in Javascript

In this approach,Using the split method, we break a string into an array at each occurrence of a specified character. Then, with the join method, we reconstruct the array into a new string, effectively removing that character.

Syntax:

let result = inputStr.split(removeStr).join('');

Example: This example shows the use of the above-explained approach.

Javascript
let inputStr = "Hello, Geeks!";
let removeStr = "e";
let result = inputStr.split(removeStr).join('');
console.log(result);

Output
Hllo, Gks!

Remove all occurrences of a character in a string using JavaScript

Removing all occurrences of a character in a string means eliminating every instance of a particular character from the given string, resulting in a modified string without that character.

Similar Reads

Approaches to Remove all occurrences of a character in a string using JavaScript

Table of Content Approach 1: Using a JavaScript Regular ExpressionApproach 2: Using the split() and join() methods Approach 3: Using for..in loopApproach 4: Using String.prototype.replace() with a FunctionApproach 5: Using Array.prototype.filter() Method...

Approach 1: Using a JavaScript Regular Expression

In this approach, Using a regular expression, we create a pattern to match all occurrences of a specific character in a string and replace them with an empty string, effectively removing that character....

Approach 2: Using the split() and join() methods

In this approach,Using the split method, we break a string into an array at each occurrence of a specified character. Then, with the join method, we reconstruct the array into a new string, effectively removing that character....

Approach 3: Using for..in loop

In this approach, for…in loop iterates through characters of the input string. If a character doesn’t match the specified one, it’s appended to a new string, effectively removing the specified character....

Approach 4: Using String.prototype.replace() with a Function

Using String.prototype.replace() with a function involves passing a regular expression to match the character to be removed globally. Inside the replace function, return an empty string to remove each occurrence of the character....

Approach 5: Using Array.prototype.filter() Method

In this approach, the input string is converted into an array of characters using the split(”) method. Then, the filter() method is used to create a new array excluding the specified character. Finally, the join(”) method is used to reconstruct the array back into a string....