HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Pad a string with a character

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptpadwithcharacterstring

Problem

Oftentimes, when working with string, especially when printing to the console, you want to pad a string with a specific character to make it a certain length. This can be useful when you want to align text in a table or when you want to make sure that all strings have the same length.
While JavaScript's String.prototype.padStart() and String.prototype.padEnd() built-in methods can be used to pad a string on one side and up to a certain length, you will need to combine them to pad both sides of a string. This is fairly simple, as you only need to calculate the number of characters to pad on each side.

Solution

const pad = (str, length, char = ' ') =>
  str.padStart((str.length + length) / 2, char).padEnd(length, char);

pad('cat', 8); // '  cat   '
pad(String(42), 6, '0'); // '004200'
pad('foobar', 3); // 'foobar'

Code Snippets

const pad = (str, length, char = ' ') =>
  str.padStart((str.length + length) / 2, char).padEnd(length, char);

pad('cat', 8); // '  cat   '
pad(String(42), 6, '0'); // '004200'
pad('foobar', 3); // 'foobar'

Context

From 30-seconds-of-code: pad-string

Revisions (0)

No revisions yet.