How to use split() and join() method In Javascript
This method involves splitting the string into an array of substrings using the specified separator (the text to be removed), then joining the array back into a string without the removed text.
Example: Here’s how you can implement this approach:
// Function to remove text
function removeText() {
// Input string
let originalText = 'w3wiki';
// Using split and join to remove text
let newText = originalText.split('Geeks').join('');
// Display output
console.log(newText);
}
// Function call
removeText();
Output
For
How to remove text from a string in JavaScript ?
We will have a string and we need to remove the given text from that string using JavaScript. We need to print the new string in the console.
These are the following methods to Remove Text from a String:
Table of Content
- Method 1: Using replace() method
- Method 2: Using replace() method with Regex
- Method 3: Using substr() method
- Method 4: Using replaceAll() method
- Method 5: Using split() and join() method