Nodejs String Format format()

Here you can find the source of format()

Method Source Code

//this method is to enhance the string object to give it tokenized, multiple argument 
//string formatting functionality much like C#'s String.format("{0}", string) method
String.prototype.format = function () {
    var formatted = this;
    for (var arg in arguments) {
        formatted = formatted.replace("{" + arg + "}", arguments[arg]);
    }// w  w  w .  j  a v  a 2  s  . c  om
    return formatted;
};

Related

  1. format()
    String.prototype.format = function () {
        var result = this.toString();
        var arg = arguments.length == 1 && typeof arguments[0] == 'object' ? arguments[0] : arguments;
        for (var value in arg) {
            result = result.split('{' + value + '}').join(arg[value]);
        return result;
    
  2. format()
    String.prototype.format = function () {
        var formatted = this;
        for (var i = 0; i < arguments.length; i++) {
            var regexp = new RegExp('\\{' + i + '\\}', 'gi');
            formatted = formatted.replace(regexp, arguments[i]);
        return formatted;
    };
    String.prototype.contains = function (it) { return this.indexOf(it) != -1; };
    ...
    
  3. format()
    'use strict';
    String.prototype.format = function() {
        var content = this;
        for (var i = 0; i < arguments.length; i++) {
            var replacement = '{' + i + '}';
            content = content.replace(replacement, arguments[i]);
        return content;
    };
    ...
    
  4. format()
    String.prototype.format = function() {
        var newStr = this, i = 0;
        while (/%s/.test(newStr)) {
            newStr = newStr.replace("%s", arguments[i++])
        return newStr;
    
  5. format()
    String.prototype.format = function () {
        if (arguments.length === 0)
            return this;
        for (var s = this, i = 0; i < arguments.length; i++)
            s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
        return s;
    };
    
  6. format()
    String.prototype.format = function () {
        var formatted = this;
        for (var i = 0; i < arguments.length; i++) {
            var regexp = new RegExp('\\{' + i + '\\}', 'gi');
            formatted = formatted.replace(regexp, arguments[i]);
        return formatted;
    };
    String.prototype.replaceAll = function (find, replace) {
    ...
    
  7. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function() {
            return args[arguments[1]];
        });
    };
    
  8. format()
    String.prototype.format = function () {
      var formatted = this;
      for (var prop in arguments[0]) {
        var regexp = new RegExp('\\{' + prop + '\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[0][prop]);
      return formatted;
    };
    if (typeof String.prototype.trim !== 'function') {
    ...
    
  9. format()
    String.prototype.format = function() {
        var formatted = this;
        for (var i = 0; i < arguments.length; i++) {
            var regexp = new RegExp('\\{'+i+'\\}', 'gi');
            formatted = formatted.replace(regexp, arguments[i]);
        return formatted;
    };