If so, what is it?
I don't mind if it involves using an external library.
Here my super simple solution which can handle %s, %d and %04d (change 4 on any number)
function sprintf(str) {
var args = arguments, i = 1;
return str.replace(/%(s|d|0\d+d)/g, function (x, type) {
var value = args[i++];
switch (type) {
case 's': return value;
case 'd': return parseInt(value, 10);
default:
value = String(parseInt(value, 10));
var n = Number(type.slice(1, -1));
return '0'.repeat(n).slice(value.length) + value;
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With