String.prototype.padLeft / String.prototype.padRight

Claude Pache claude.pache at gmail.com
Mon Nov 16 12:51:19 UTC 2015


Here are my typical use cases (found by scanning uses of "str_pad" in my PHP codebase):

* transferring data through a protocol that uses fix-length fields;
* formatting things like date/hours, e.g. "08:00" for "8am";
* generating filenames of fixed length, so that they sort correctly, e.g. "foo-00042.txt";
* generating codes of fixed length (e.g. barcodes).

In all those cases, the set of characters is typically limited to ASCII or ISO-8559-1.
Moreover, the filler string consists always of one ASCII character (usually " " or "0").

—Claude



More information about the es-discuss mailing list