How to remove spaces from a string using JavaScript?
We will explore how to remove spaces from a string using JavaScript. We’ll take a string as input and then remove all the spaces present in it, returning the resulting string.
These are the following methods by using we can remove spaces from a string using JavaScript:
Examples of remove spaces from a string using JavaScript
1. Using split() and join() Methods
- The split() method is used to split a string into multiple sub-strings and return them in the form of an array.
- The join() method is used to join an array of strings using a separator.
- This will return a new string with the joined string using the specified separator.
- This method is used on the returned array and no separator (“”) is used to join the strings. This will join the strings in the array and return a new string.
- This will remove all the spaces in the original string.
Example: In this example, we will use the split() and join() Methods.
let originalText = "Beginner for Beginner Portal";
let removedSpacesText = originalText
.split(" ")
.join("");
console.log(removedSpacesText);
Output
w3wikiPortal
2. Using the replace() method
We can use the replace()
method with a regular expression to globally replace all space occurrences with an empty string:
Example: In this example, we will use replace() method with regex.
let originalText = "Beginner for Beginner Portal";
let removedSpacesText = originalText.replace(
/ /g,
""
);
console.log(removedSpacesText);
Output
w3wikiPortal
3.Using the reduce() method
The reduce()
method, combined with the spread operator, allows us to remove spaces from a string. It iterates through each character, excluding spaces, and accumulates the non-space characters:
Example: In this example, we will use reduce() method with spread operator
let originalText = "Beginner for Beginner Portal";
let removedSpacesText = [...originalText].reduce(
(accum, char) =>
char === " " ? accum : accum + char,
""
);
console.log(removedSpacesText);
Output
w3wikiPortal
4. Using the trim() method
The trim()
method removes leading and trailing whitespace from a string. It’s useful when dealing with user input or data from external sources:
Example: In this example, we will remove the space from the start and end of the string using the trim() method.
const geek = " w3wiki ";
console.log(geek.trim());
// Expected output: "w3wiki";
Output
w3wiki
5. Using Lodash _.trim() Method
In this approach we are using _.trim() method which trim the given string and return the string having no space.
Example: This example is the implementation of the above-explained approach.
// Defining Lodash variable
const _ = require("lodash");
let str = " Beginner-for-Beginner ";
// Using _.trim() method
console.log(_.trim(str));
Output:
Beginner-for-Beginner
6. Using Regular Expressions with String.prototype.replace()
Regular expressions offer a powerful way to manipulate strings in JavaScript. By utilizing the replace() method with a regular expression pattern matching all spaces (/\s/g), we can globally replace all space occurrences with an empty string, effectively removing all spaces from the string.
Example:
const stringWithSpaces = "Hello, World! This is a string with spaces.";
const stringWithoutSpaces = stringWithSpaces.replace(/\s/g, '');
console.log(stringWithoutSpaces); // Output: Hello,World!Thisisastringwithspaces.
Output
Hello,World!Thisisastringwithspaces.
Contact Us