Posted By

edektor on 01/10/10


Tagged

javascript String Pad padding


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

nerdfiles


String.prototype.pad


 / Published in: JavaScript
 

pads a string "2".pad(2, "0"); //02

  1. String.prototype.pad = function(l, s){
  2. return (l -= this.length) > 0
  3. ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, s.length) + this + s.substr(0, l - s.length)
  4. : this;
  5. };

Report this snippet  

You need to login to post a comment.