Skip to content

Instantly share code, notes, and snippets.

@orhanveli
Created November 14, 2011 20:50
Show Gist options
  • Save orhanveli/1365115 to your computer and use it in GitHub Desktop.
Save orhanveli/1365115 to your computer and use it in GitHub Desktop.
c# like string.format() function for JavaScript
String.prototype.format = function () {
var s = this,
i = arguments.length;
while (i--) {
s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
}
return s;
};
//useage
var output = 'Hello {0}, how are you today? if you {1} let me know :)'.format('Orhan', 'not ok');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment