String.format = String.format || function (format) {
var params = [].splice.call(arguments, 1), nextOpen, nextClose = 0, index, plValue, spaces, totalSpaces;
while ((nextOpen = format.indexOf('{', nextClose)) >= 0) {
if (isNaN(+format[nextOpen+1])) {
nextClose = nextOpen + 1;
continue;
}
nextClose = format.indexOf('}', nextOpen);
index = format.substring(nextOpen + 1, nextClose);
if (index.indexOf(',') > 0) {
spaces = +index.substring(index.indexOf(',') + 1);
index = +index.substring(0, index.indexOf(','));
}
plValue = params[index] + '';
if (spaces) {
totalSpaces = new Array(Math.abs(spaces) - plValue.length + 1).join(" ");
plValue = spaces > 0 ? totalSpaces + plValue : plValue + totalSpaces;
}
format = format.substring(0, nextOpen) + ((!plValue && plValue != 0) ? "" : plValue) + format.substring(nextClose + 1);
spaces = 0;
}
return format;
};
Usage
String.format("Hello {0} {1}", "Andreas", "Grech"); // "Hello Andreas Grech"
for (var i = 0; i <= 1000; i += 200) {
String.format("{0, 5}. Something", i);
}
/* Output:
0. Something
200. Something
400. Something
600. Something
800. Something
1000. Something
*/
String.format("Padded from {0, 7} left", "the"); // "Padded from the left"
You can fork this project at github: https://github.com/dreasgrech/JSStringFormat
