How to usestring.trim() method in Javascript
Here, we will be using the trim() method. We can strip a string using the trim() method and remove unnecessary trailing and leading spaces and tabs from the string.
Example: In this example, we will remove the extra spaces from the string using the trim method of JavaScript.
Javascript
const string = " w3wiki " ; console.log( "Before string:'" + string + "'" ); console.log( "After string :'" + string.trim() + "'" ); |
Before string:' w3wiki ' After string :'w3wiki'
String strip() in JavaScript
In this article, the task is to remove all leading and trailing spaces and tabs from the string. We will use a method to reach the goal.
These are the following methods:
Table of Content
- Using string.trim() method
- Using the Javascript string replace() method
- Using split(), filter() and join() Method
- Using Lodash _.trim() Method