Last active
April 11, 2016 05:14
-
-
Save anon5r/27a768294623651fe5dc6434c0678ff8 to your computer and use it in GitHub Desktop.
Padding string fixed length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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