Something that happens to me very often is that I need to format a string, for this I end up manually concatenating several strings, my question is if there is any implementation of printf in javascript
for those who don't know what to doprintf
printf("Color %s, numero1 %d, numero2 %05d, hex %x, real %5.2f.\n", "rojo", 12345, 89, 255, 3.14);
would return:
Color rojo, numero1 12345, numero2 00089, hex ff, real 3.14.
Update
At the moment I comment that I found a similar but not exact solution to the problem.
String.prototype.format = function() {
var formatted = this;
for( var idx in arguments) {
formatted = formatted.replace(new RegExp("\\{" + idx + "\\}", 'g'), arguments[idx]);
}
return formatted;
};
Usage example:
"hola usuario de {1}, hoy es {0}".format('viernes', 'Stack Overflow ES')
Since ES6 javascript supports string interpolation something like printf in c++
note that the backtick " ` " must be used instead of single or double quotes
At the end of searching I found this sprintf library that does what I needed. Thank you very much anyway.