Open In App

Lodash _.trim() Method

Last Updated : 25 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

_.trim(string, chars);

Parameters:

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

Return Value:

This method returns a trimmed string

Example 1: In this example, we are trimming the given string by the use of the _.trim() method.

Javascript




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


Output:

Geeks-for-Geeks

Example 2: In this example, we are trimming the given string by “-” by the use of the _.trim() method.

Javascript




// Defining Lodash variable
const _ = require('lodash');
 
let 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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads