Skip to content

Instantly share code, notes, and snippets.

@fredrick
Created May 16, 2011 22:28
Show Gist options
  • Select an option

  • Save fredrick/975520 to your computer and use it in GitHub Desktop.

Select an option

Save fredrick/975520 to your computer and use it in GitHub Desktop.

Revisions

  1. Fredrick Galoso created this gist May 16, 2011.
    11 changes: 11 additions & 0 deletions String.format.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    /*
    * Extend JavaScript String object with printf() like functionality
    * "{0} is dead, but {1} is alive!".format("This", "that");
    */
    String.prototype.format = function() {
    var formatted = this;
    for(arg in arguments) {
    formatted = formatted.replace("{" + arg + "}", arguments[arg]);
    }
    return formatted;
    };