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.

Community
  • 1
  • 1
Daniel Reis
  • 12,944
  • 6
  • 43
  • 71

3 Answers3

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.

Community
  • 1
  • 1
Daniel Reis
  • 12,944
  • 6
  • 43
  • 71
  • 8
    Nice one liner but breaks when the string is longer than the padding. – Daniel Feb 14 '15 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 '15 at 11:57
  • 1
    or using `.substr()` instead of array.slice(), borrowing from mogsdad on http://stackoverflow.com/questions/5366849/convert-1-to-0001-in-javascript `('0000'+str).substring(Math.min((""+str).length,4))` – gordon Feb 28 '17 at 18:50
  • 5
    '01'.padStart(4).replace(' ',0) – Allan Felipe Murara Mar 01 '17 at 02:39
22
function pad(value, length) {
    return (value.toString().length < length) ? pad("0"+value, length):value;
}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 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 08 '15 at 20:45
  • You forgot to reduce length by 1 when recursing. – Vicky Chijwani Jun 07 '16 at 11:18
  • @VickyChijwani Could you explain a little more about why that would benefit this solution? – Kevin Bowersox Jun 08 '16 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()`. – Vicky Chijwani Jun 09 '16 at 08: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. – Vicky Chijwani Jun 09 '16 at 09: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"
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • +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 '16 at 02:24
  • Sorry, I made a mistake: It should be `return str + this;`. Alternatively, I have posted an even more optimized and faster version: http://stackoverflow.com/a/39030416/1854856 – StanE Aug 19 '16 at 03:03