Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Lodash _.trim() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The Lodash _.trim() Method removes leading and trailing white spaces or specified characters from the given string.

Syntax:

_.trim( string, chars )

Parameters: This method accepts two parameters as mentioned above and described below:

  • string: This parameter holds the string that need to be trimmed.
  • chars: This parameter holds the charset to be specifically trimmed.

Return Value: This method returns a trimmed string

Example 1:

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
var str = "   Geeks-for-Geeks   ";
  
// Using _.trim() method
console.log(_.trim(str));

Output:

Geeks-for-Geeks

Example 2: explicitly trimming “-” from the string.

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
var str = "----GeeksforGeeks----";
  
// Using _.trim() method
console.log(_.trim(str, "-"))

Output:

GeeksforGeeks

Note: This will not work in normal JavaScript because it requires the lodash library to be installed and can be installed using npm install lodash.

My Personal Notes arrow_drop_up
Last Updated : 18 Sep, 2020
Like Article
Save Article
Similar Reads
Related Tutorials