At times while working in javascript, we need to remove spaces from a string. This article will illustrate how to remove all the spaces from a string in javascript using different methods and examples.
Table of Contents:-
- Javascript string remove spaces using replace() and RegExp
- Javascript string remove spaces using replaceAll()
- Javascript string remove spaces using split() and join()
Javascript string remove spaces using replace() and Regex
RegExp is the regular expressions object. These objects are patterns used to match the character combinations within a text/string.
replace() method: Javascript provides a method to replace a part or entire string with a replacement from a pattern and returns a new string.
Example:-
Remove spaces from the string: “Javascript is a popular language”
Code:-
Frequently Asked:
let dummyString = "Javascript is a popular language" let stringWithoutSpaces = dummyString.replace(/\s+/g, '') console.log("Original String: "+ dummyString ) console.log("Final String: "+ stringWithoutSpaces)
Here, in the above code, the replace() method will remove all the spaces from the string dummyString.
The first parameter passed is the regular expression, which needs to be replaced from the string on which the replace() method is applied. In our case, the pattern is (\s+) indicating spaces, and the replace() method is applied on dummyString. This parameter can be a regular expression or a string.
The second parameter passed in the replace() method is the replacement. In our example, it should replace all the spaces with (”) nothing.
Output:-
Original String: Javascript is a popular language Final String: Javascriptisapopularlanguage
Javascript string remove spaces using replaceAll()
The replaceAll() method in javascript replaces all occurrences of a pattern in a string with a replacement and returns a new string.
Example:-
Remove spaces from the string: “Javascript is a popular language”
Code:-
let dummyString = "Javascript is a popular language" let stringWithoutSpaces = dummyString.replaceAll(' ', '') console.log("Original String: "+ dummyString ) console.log("Final String: "+ stringWithoutSpaces)
Here, in the above code, the replaceAll() method will remove all the spaces from the string dummyString.
The first parameter passed is a substring from the string on which the replace() method is applied. In our case, the substring is (” “) indicating a space, and the replace() method is applied on dummyString.
The second parameter passed in the replace() method is the replacement. That is it should replace all the spaces with (”) nothing.
Output:-
Original String: Javascript is a popular language Final String: Javascriptisapopularlanguage
Javascript string remove spaces using split() and join()
The split() method in javascript returns an array of substrings formed by splitting a given string.
The join() method in javascript joins the elements of the array back into a string.
Code:-
let dummyString = "Javascript is a popular language" let stringWithoutSpaces = dummyString.split(' ').join('') console.log("Original String: "+ dummyString ) console.log("Final String: "+ stringWithoutSpaces)
Here, in the above code, we split the original string into an array of substrings using the split() method by space (” “).
For example, in our case, the array of substrings will contain elements {Javascript, is, a, popular, language}.
Finally, using the join() method, these array elements are joined back to form a string without spaces “Javascriptisapopularlanguage”
Output:-
Original String: Javascript is a popular language Final String: Javascriptisapopularlanguage
Read More:
- Remove character at a specific index from a string in javascript
- Javascript: How to remove text from string
- Javascript: Remove last character of string
- Javascript: Remove first character from a string
I hope this article helped you to delete all the spaces from a javascript string. Good Luck !!!