Open In App
Related Articles

Lodash _.pad() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

Lodash_.pad() method is used to pad the string on left and right sides if it is shorter than the given length. If the length can’t be divided evenly then padding characters are truncated.

Syntax:

_.pad([string=''], [length=0], [chars=' '])

Parameters:

This method accepts three parameters as mentioned above and described below:

  • [string=”] (string): This parameter holds the string that need to pad.
  • [length=0] (number): This parameter holds the padding length.
  • [chars=’ ‘] (string): This parameter holds the string that is used as padding.

Return Value:

This method returns the padded string.

Example 1: In this example, the _.pad function from the lodash library is used to pad strings with specified characters to reach a desired length.

Javascript




const _ = require('lodash');
 
let str1 = _.pad("GEEKS", 25, '-*-');
console.log(str1);
 
let str2 = _.pad("GFG--Geeks", 30, 'gfg');
console.log(str2);


Output:

"-*--*--*--GEEKS-*--*--*--"
"gfggfggfggGFG--Geeksgfggfggfgg"

Example 2: This code uses the Lodash library to pad three strings with different characters to a specified length using the _.pad() method.

Javascript




const _ = require('lodash');
 
let str1 = _.pad("GEEKS__FOR__GEEKS", 20, '_');
console.log(str1);
 
let str2 = _.pad("G4G--geeks--GEEKS", 25, '*');
console.log(str2);
 
let str3 = _.pad("GEEKS-----FOR_____Geeks", 20);
console.log(str3);


Output:

"_GEEKS__FOR__GEEKS__"
"****G4G--geeks--GEEKS****"
"gEEKS-----FOR_____Geeks"
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 Nov, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials