Format a string with an array of arguments


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Format a string with an array of arguments.
  3.  *
  4.  * @param msg A string.
  5.  * @return A formatted string
  6.  */
  7. function format(msg) {
  8. if (arguments && arguments.length > 1) {
  9. for (var i = 1; i < arguments.length; i++) {
  10. msg = msg.replace('{'+(i-1)+'}', arguments[i]);
  11. }
  12. }
  13. return msg;
  14. }

URL: http://mariuzzo.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.