Removing whitespaces from a string can be complex when it comes to tabs and line breaks and while you’re working with various lines of codes but JavaScript provides users with methods through which it becomes easier to manipulate strings and remove whitespaces from them and that’s why in this article we’ll discuss different methods and ways to remove whitespaces from a string in JavaScript, their explanation and how they’re used along with examples.
Removing Whitespaces from a String
User can remove whitespaces from a string using different built-in methods in JavaScript, some of them are listed below:
- Regular expression
- split() with join()
- trim()
Let’s understand each of these one by one, how they work along with examples for your better understanding.
Regular Expression
In JavaScript the method “string.replaceAll()” or “string.replace()” accepts a regular expression to find matches with the provided string. Below is the example of a string where we used these methods to remove all the whitepsaces in the string and the g switch is used to search and replace the spaces from the entire string.
Example:
console.log(example); //without using the method
console.log(example.replace(/ /g,'')); //with the replace() method
Output:
In order to remove all the whitespaces and not just the space character then you can use “\s” which will match against all the newline character, space character, tab character and translate it into a simpler code.
Example:
console.log(example);
console.log(example.replace(/\s/g,''));
Output:
spit() with join()
Another way to remove all the whitespaces from a string in JavaScript is splitting the string at a space character and then joining it back again. This can be done using the split() method along with the join() method as demonstrated through the below example.
Example:
console.log(example.split(' ').join(''));
Output:
You can also use \s for removing all the whitespace characters from the string.
Example:
console.log(example.split(/\s+/).join(''));
Output:
trim()
Another method that JavaScript provides for removing all the whitespaces including space, tabs, no-break space and all the line terminator characters is trim(). This method can remove all the whitespaces from the provided string but if you only want to remove the whitespaces from the beginning or the end of the string then you can use trimStart() and trimEnd() methods to specify where to remove the whitespaces form.
Example:
console.log(example.trim());
Output:
To remove whitespaces from only the beginning or end of the string we’ll use trimStart() and trimEnd() methods.
Example:
console.log(example.trimStart());
console.log(example.trimEnd());
Output:
Conclusion
Removing whitespaces can be a difficult task when you’ve hundreds of lines of code. That’s why JavaScript provides various methods for removing these whitespaces. In this article we discussed various ways of removing whitespaces from strings, some of these methods remove the overall space characters and with some method we can specify where we want the space to be removed. Examples along with code is provided for each method for your better understanding.
from https://ift.tt/3Dh8YzP
0 Comments