Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Pad //JavaScript Function

DZone's Guide to

Pad //JavaScript Function

·
Free Resource


[UPDATED CODE AND HELP CAN BE FOUND HERE]



usage:


var s = "Jonas";
document.write(
  s, " => ", s.pad(20, "[]", 0), "
", s, " => ", s.pad(20, "[====]", 1), "
", s, " => ", s.pad(20, "~", 2) );
code: //+ Jonas Raoni Soares Silva //@ http://jsfromhell.com/string/pad [v1.0] String.prototype.pad = function(l, s, t){ return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2)) + this + s.substr(0, l - t) : this; };
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}