Skip to content

Instantly share code, notes, and snippets.

@anon5r
Last active April 11, 2016 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anon5r/27a768294623651fe5dc6434c0678ff8 to your computer and use it in GitHub Desktop.
Save anon5r/27a768294623651fe5dc6434c0678ff8 to your computer and use it in GitHub Desktop.
Padding string fixed length
// Pad string to right
String.prototype.pad=function(len,str){
if(typeof str=="undefined") str=" ";
if(this.length>len) return this.toString();
str=str.repeat(len-this.length);
return (this+str).substr(0,len);
}
// Pad string to left
String.prototype.lpad=function(len,str){
if(typeof str=="undefined") str=" ";
if(this.length>len) return this;
str=str.repeat(len);
return (str+this).slice(len*-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment