Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
printf JavaScript
if (!String.format) {
/**
* printf function
* @link http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format
* @example var fullName = String.format('{0} {1}', firstName, lastName);
*
* @param {string} format String with replacable parameters
* @returns {string} formatted string
*/
String.format = function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment