Inject parameters into a string


/* 
 * Formats a string with the given parameters. The string to format must have
 * placeholders that correspond to the index of the arguments passed and surrounded 
 * by curly braces (e.g. 'Some {0} string {1}').
 *
 * @param {string} var_args The string to be formatted should be the first 
 *     argument followed by the variables to inject into the string
 * @return {string} The string with the specified parameters injected
 */
format = function(var_args){
    var args = Array.prototype.slice.call(arguments, 1);
    return var_args.replace(/\{(\d+)\}/g, function(m, i){
        return args[i];
    });
};

format("Is it {0}->{1} or {1}->{0}?", "chicken", "egg") == "Is it chicken->egg or egg->chicken?"

1 comment:

Tech said...

Augurs Technologies in Germany is the world's leading software development company and information technology services provider specializing in mobile application development, software development, web design, and digital marketing in Germany. We provide quality-oriented services around the world and strive to provide the best solution in the industry that will bring a remarkable experience to your mobile app, modern design with the latest technologies.Augurs Technologies Germany
. Our technology expertise, excellent delivery and passionate team have earned us recognition as one of the most outstanding mobile application and software development companies in Germany.