Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used?
3.
Syntax, Parameter and Return Value
3.1.
Syntax
3.2.
Parameters
3.3.
Return Value
4.
Examples 
4.1.
Basic Padding of a String
4.2.
JavaScript
4.3.
Padding with Custom Characters
4.4.
JavaScript
4.5.
Aligning Text for Display
4.6.
JavaScript
4.7.
Formatting for Fixed-Width Output
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.pad() handle strings longer than the specified length?
5.2.
Can _.pad() pad strings with different characters on each side?
5.3.
Is the padding applied evenly on both sides?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.pad() Method

Author Rinki Deka
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

String formatting often requires adjusting the length of a string by adding padding characters to its beginning and end. Lodash's _.pad() method offers a convenient way to do this. It pads a string on both sides evenly (or as evenly as possible), ensuring that it reaches a specified length. 

Lodash _.pad() Method

This function is particularly useful in scenarios like aligning text output, formatting data for display, or preparing strings for fixed-width file formats.

Why This Function is Used?

The _.pad() function is used to extend the length of a string to a specified total length by adding padding characters evenly to both the start and end of the string. This is essential in text formatting where a uniform string length is required, such as in creating tabular data displays, aligning log messages, or formatting text for UI elements.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax

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

Parameters

  • [string=''] (string): The string to pad.
     
  • [length=0] (number): The padding length.
     
  • [chars=' '] (string): The string used as padding.

Return Value

 (string) - Returns the padded string.

Examples 

Basic Padding of a String

  • JavaScript

JavaScript

var _ = require('lodash');

var text = 'hello';

console.log(_.pad(text, 10));

Output:

 '  hello   '


Demonstrates padding a string to a total length of 10 characters.

Padding with Custom Characters

  • JavaScript

JavaScript

var title = 'title';

console.log(_.pad(title, 10, '-='));

Output:

 '-=title=-='


Shows how to pad a string with a custom character sequence.

Aligning Text for Display

  • JavaScript

JavaScript

var names = ['Alice', 'Bob', 'Carol'];

var paddedNames = names.map(name => _.pad(name, 10));

console.log(paddedNames.join('\n'));

Output:

 '  Alice   '
'   Bob    '
 '  Carol   '


An example of using _.pad() to align a list of names in a column.

Formatting for Fixed-Width Output

  • JavaScript

JavaScript

var data = ['123', '45', '6789'];

var formattedData = data.map(d => _.pad(d, 6, '0'));

console.log(formattedData.join('\n'));

Output:

'001230'
'000450'
'067890'


Demonstrates using padding to format numbers for a fixed-width output.

Frequently Asked Questions

How does _.pad() handle strings longer than the specified length?

If the string is already longer than the specified length, _.pad() returns the original string without any modifications.

Can _.pad() pad strings with different characters on each side?

_.pad() uses the same characters for both sides. For different characters on each side, you would need to use _.padStart() and _.padEnd() separately.

Is the padding applied evenly on both sides?

_.pad() attempts to pad evenly on both sides. If the padding can't be divided evenly, the extra character is added to the end of the string.

Conclusion

Lodash's _.pad() method is an effective tool for extending the length of a string by adding padding characters symmetrically to both sides. It's useful for text formatting, aligning output, and preparing strings for specific display or file format requirements.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass