54

Possible Duplicate:
Is there a JavaScript function that can pad a string to get to a determined length?

What's the simplest way to left pad a string in javascript?

I'm looking for an inline expression equivalent to mystr.lpad("0", 4): for mystr='45' would return 0045.

0

3 Answers 3

165

Found a simple one line solution:

("0000" + n).slice(-4)

If the string and padding are in variables, you would have:

mystr = '45'
pad = '0000'
(pad + mystr).slice(-pad.length)

Answer found here, thanks to @dani-p. Credits to @profitehlolz.

5
  • 8
    Nice one liner but breaks when the string is longer than the padding.
    – Daniel
    Feb 14, 2015 at 21:16
  • 7
    Use this if mystr could be longer than the pad string and you don't want mystr to be truncated. (pad + mystr).slice(-Math.max(pad.length, mystr.length))
    – Bruno
    Feb 18, 2015 at 11:57
  • 1
    or using .substr() instead of array.slice(), borrowing from mogsdad on stackoverflow.com/questions/5366849/… ('0000'+str).substring(Math.min((""+str).length,4))
    – gordon
    Feb 28, 2017 at 18:50
  • 6
    '01'.padStart(4).replace(' ',0) Mar 1, 2017 at 2:39
  • '01'.padStart(4).replaceAll(' ',0) otherwise will return "0 01". And actually can now just use '01'.padStart(4, "0")
    – AJP
    Dec 5, 2023 at 16:34
22
function pad(value, length) {
    return (value.toString().length < length) ? pad("0"+value, length):value;
}
5
  • 2
    Elegant solution. One suggestion: this function can return a number instead of a string if in the first call to the function a number is passed to value, and value.toString().length < length. A simple fix would be: code return (value.toString().length < length ? pad("0"+value, length) : value.toString()
    – Jacob Lee
    May 8, 2015 at 20:45
  • You forgot to reduce length by 1 when recursing. Jun 7, 2016 at 11:18
  • @VickyChijwani Could you explain a little more about why that would benefit this solution? Jun 8, 2016 at 23:54
  • Well, won't the function go into an infinite loop if length never reaches 0? Which means it must be decreased by at least 1 in every call to pad(). Jun 9, 2016 at 8:18
  • @KevinBowersox sorry, my bad, length does not need to reach 0 because the termination condition is value.toString().length < length and value grows by 1 in every call. Jun 9, 2016 at 9:42
9

Something like below:

String.prototype.lpad = function(padString, length) {
    var str = this;
    while (str.length < length)
        str = padString + str;
    return str;
}
console.log('45'.lpad('0', 4)); // "0045"
2
  • +1. But appeding data to the beginning is never a good idea. The data will then have to be reallocated everytime. Instead, append always to the end only. Following small improvement is ~3 times faster: String.prototype.lpad = function(padString, length) { var str=''; while (str.length < length) { str += padString; } return this + str; }
    – StanE
    Aug 19, 2016 at 2:24
  • Sorry, I made a mistake: It should be return str + this;. Alternatively, I have posted an even more optimized and faster version: stackoverflow.com/a/39030416/1854856
    – StanE
    Aug 19, 2016 at 3:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.