Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 497 Bytes

pad-string-on-both-sides.md

File metadata and controls

18 lines (16 loc) · 497 Bytes
title description author tags
Pad String on Both Sides
Pads a string on both sides with a specified character until it reaches the desired length.
axorax
string,pad,manipulation
function padString(str, length, char = ' ') {
  const totalPad = length - str.length;
  const padStart = Math.floor(totalPad / 2);
  const padEnd = totalPad - padStart;
  return char.repeat(padStart) + str + char.repeat(padEnd);
}

// Usage:
padString('hello', 10, '*'); // Returns: '**hello***'