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***'