Javascript String format()
String.prototype.format = String.prototype.f = function() { var s = this, i = arguments.length; while (i--) s = s.replace(new RegExp("\\{" + i + "\\}", "gm"), arguments[i]); return s;/* www . j a va 2 s . c o m*/ };
/**/* ww w . j av a2 s . c om*/ * Replaces %1, %2 and so on in the string to the arguments. * * @method String.prototype.format * @param {Any} ...args The objects to format * @return {String} A formatted string */ String.prototype.format = function() { var args = arguments; return this.replace(/%([0-9]+)/g, function(s, n) { return args[Number(n) - 1]; }); };
String.prototype.format = function() { a = this;/*from www . j a v a2s . c om*/ for (k in arguments) { a = a.replace("{" + k + "}", arguments[k]) } return a }
String.prototype.format = function() { var placeholderREG = /\{(\d+)\}/g; var args = arguments; return this.replace(placeholderREG, function(holder, num) { return args[num]; });//from w w w .j a v a 2 s . c o m }
String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number]//from w w w .j a v a 2s.c om : match ; }); };
String.prototype.format = function() { var args = arguments; return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function(m, n) { if (m == "{{") { return "{"; }// w ww .jav a 2s. c o m if (m == "}}") { return "}"; } return args[n]; }); };
String.prototype.format = function () { var formatted = this; for (var arg in arguments) { formatted = formatted.replace("{" + arg + "}", arguments[arg]); }/*from w w w .ja v a2 s. com*/ return formatted; };
String.prototype.format = function(){ var pattern = /\{\d+\}/g; var args = arguments; return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; }); }
String.prototype.format = function() { a = this;/*from w ww .ja va 2 s .c o m*/ for (k in arguments) { a = a.replace("{" + k + "}", arguments[k]); } return a; };
String.prototype.format = String.prototype.format = function(){ var s = this, i = arguments.length; while (i--){/*from w w w .ja v a 2 s. com*/ s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); } return s; };
String.prototype.format = function() { var newStr = this, i = 0; while (/%s/.test(newStr)) newStr = newStr.replace("%s", arguments[i++]) return newStr; }
String.prototype.format = function () { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : ''; });//from w w w. ja va 2 s.c o m };
String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function (m, n) { return args[n]; }); };
String.prototype.format = function() { var args = arguments; return this.replace(/{(\d{1})}/g, function() { return new String(args[arguments[1]]); });//from ww w .j av a 2 s . c om };
// Code goes here String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function() { return args[arguments[1]]; });// w ww . java 2 s . c o m };
String.prototype.format = function() { var newStr = this, i = 0; while (/%s/.test(newStr)) { newStr = newStr.replace("%s", arguments[i++]) }//from w ww . java 2 s. c om return newStr; }
String.prototype.format = function() { var formatted = this; _.forEach(arguments, function(arg, i) { formatted = formatted.replace("{" + i + "}", arg); });/*from ww w . j a v a 2s . c om*/ return formatted; };
'use strict';//from ww w . j a v a2s .com String.prototype.format = function() { var THIS = this; [].forEach.call(arguments, function(a, i){ THIS = THIS.replace(new RegExp('[\\{]' + i + '[\\}]', 'gi'), a); }); return THIS; };
String.prototype.format = function () { var args = arguments; if (args && args.length > 0) { return this.replace(/\{(\d+)\}/g, function (term, index) { return args[index] || ''; });/* w ww. j a v a 2 s.co m*/ } return this; };
String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number]/*from w ww . jav a 2 s .c o m*/ : match ; }); };
'use strict';/*from w w w .j a va 2 s . co m*/ String.prototype.format = function() { var formatted = this; for (var arg in arguments) { formatted = formatted.replace('{' + arg + '}', arguments[arg]); } return formatted; };
String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function(m,i){ return args[i]; });/*from w w w. ja va 2s .c o m*/ }
var util = require('util'); String.prototype.format = function() { var args = Array.prototype.slice.call(arguments); args.splice(0, 0, this.toString()); return util.format.apply(null, args); };
String.prototype.format = function () { var args = arguments; return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) { if (m == "{{") { return "{"; } if (m == "}}") { return "}"; } return args[n]; });//from ww w . j a v a 2 s . c o m };
var util = require('util'); String.prototype.format = function () { return util.format.apply(null, [ this.toString() ].concat(Array.prototype.slice.call(arguments))); };
String.prototype.format = function() { var s = this, i = arguments.length while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]) }/* ww w . j a va 2s. c o m*/ return s }
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]); }// ww w . j av a2 s .co m return formatted; };
String.prototype.format = function(){ var args = arguments; return this.replace(/\{(\d+)\}/g, function(m,i){ return args[i]; });//ww w . j av a2 s . c o m };
String.prototype.format = function() { var str = this.toString() if (arguments.length) { const type = typeof arguments[0] const args = type === 'string' || type === 'number' ? Array.prototype.slice.call(arguments) : arguments[0] for (const arg in args) str = str.replace(new RegExp(`\\{${arg}\\}`, 'gi'), args[arg]) }/* w w w . j av a2 s . c o m*/ return str }
String.prototype.format = function() { var args = arguments[0]; // console.log(args); return this.replace(/{([a-z|A-Z|-]+)}/g, function(match, key) { return typeof args[key] != 'undefined' ? args[key]/*www . j av a2 s. c o m*/ : '' ; }); };
String.prototype.format = function(){ var n = parseFloat(this); if( isNaN(n) ) return "0"; return n.format(); };
String.prototype.format = function () { var str = this; for(var i = 0, j = arguments.length; i < j; i++){ str = str.replace(new RegExp('\\{' + i +'\\}', 'g'), arguments[i]); } /* w ww . j a v a2 s . co m*/ return str; }
// Format script with example String.prototype.format = function() { var pattern = /\{\d+\}/g; var args = arguments; return this.replace(pattern, function(capture){return args[capture.match(/\d+/)]}); } // var myName = 'John'; // var myText = "hello {0}, how are you??".format(myName); // Place variable in the string.
String.prototype.format = function(){ var content = this; for (var i=0; i < arguments.length; i++) {//from w ww. j a va2s . c om var replacement = '{' + i + '}'; content = content.replace(replacement, arguments[i]); } return content; };
String.prototype.format = function () { if (arguments.length == 0) { return this; }/*from w ww .j av a2 s . c o m*/ for (var s = this, i = 0; i < arguments.length; i++) { s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]); } return s; };
String.prototype.format = String.prototype.f = function () { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); }/* www .j av a 2s. com*/ return s; };
// Copyright (c) 2014 Christopher L. Bond. All rights reserved. // clbond@gmail.com String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match; });//from w w w.ja v a 2 s. c o m };
//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 ww. j a va2 s . com return formatted; };
String.prototype.format = function() { var args = [].slice.call(arguments); return this.replace(/%(\d+)/g, function(w, m) { var offset = parseInt(m) - 1; if (args[offset] != undefined) { return String(args[offset]); }//from w w w . j a va2 s . com return w; }); } window.onload = function() { myButtonSet = LiteButtons.fromSelector('.buttonset button.lite'); }
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]); }// ww w . ja v a 2 s .co m return formatted; };
String.prototype.format = function(){ let s = this; for (let i = 0; i < arguments.length; i++){ s = s.replace(/* w ww . j a v a2 s .com*/ new RegExp("\\{" + i + "\\}", "gm"), arguments[i] ); } return s; }
"use strict";/*from w ww. j ava 2s . c o m*/ String.prototype.format = function () { var args = arguments; return this.replace(/\{(\d+)\}/g, function () { return args[arguments[1]]; }); };
String.prototype.format = function () { var reg = /{(\d+)}/gm, args = arguments;/*from w w w .ja v a 2s . c o m*/ return this.replace(reg, function (match, name) { return args[~~name]; }); };
/* String Protorype Extension */ String.prototype.format = function() { var newStr = this, i = 0;//from ww w . jav a 2 s. c om while (/%s/.test(newStr)) { newStr = newStr.replace("%s", arguments[i++]); } return newStr; }
String.prototype.format = function () { var args = arguments; return this.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; });/*from ww w. ja va 2s. c o m*/ };
/**/*from w w w .ja va 2 s . c om*/ * Usage: "Hello, {0}!".format("World"); */ String.prototype.format = function() { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); } return s; };
'use strict';//from w ww. j ava2 s . c om String.prototype.format = function () { let str = this; for (let i = 0; i < arguments.length; i++) { let reg = new RegExp("\\{" + i + "\\}", "gm"); str = str.replace(reg, arguments[i]); } return str; };
String.prototype.format = function () { var formatted = this, i, size = arguments.length; for (i = 0; i < size; i++) { var regexp = new RegExp('\\{' + i + '\\}', 'gi'); formatted = formatted.replace(regexp, arguments[i]); }// w w w . j a v a 2 s . c o m return formatted; };
String.prototype.format = function () { var args = arguments; return this.replace(/\{(\d+)\}/g, function (m, i) { return args[i]; }/*from ww w. ja v a 2 s . c o m*/ ); };
String.prototype.format = function(){ args = arguments.length === 1 ? (arguments['0'] instanceof Array ? arguments['0'] : (typeof arguments['0'] === 'object' ? arguments['0'] : [arguments['0']])) : arguments; return this.replace(/\$(\w+)/g, function(match, idx){ return args[idx]; });/* w w w . j av a 2 s .co m*/ };
// String extension 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]); }//from w w w . j a v a 2s. c o m return formatted; }; String.prototype.contains = function (it) { return this.indexOf(it) != -1; };
'use strict';// w ww . j ava2s . c om String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { if (typeof args[number] !== 'undefined') { return args[number]; } else { return match; } }); };
'use strict';//from w w w. j a v a 2s . c o m 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; };
/**/*from www. j av a2s. com*/ * @returns String {string} * @deprecated use `string ${param}` */ String.prototype.format = function () { var args = arguments; return this.replace(/{(\d+)}/g, function (match, number) { return args[number] != undefined ? args[number] : match; }); };
//basic and kind of crappy python style formatting //could be improved String.prototype.format = function(){ var str = this; for (let i=0; i < arguments.length; i++){ str = str.replace('{' + i + '}', arguments[i]);} return str;/*from ww w .jav a 2s.c o m*/ };
String.prototype.format = function() { var str = this.toString(); if (!arguments.length) return str; var args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); for (arg in args) str = str.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return str;// ww w . java2s. com }
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.replace('{' + value + '}', arg[value]); result = result.split('{' + value + '}').join(arg[value]); }/*from w w w . j av a 2 s . com*/ return result; }
String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number]/* w w w. j ava 2 s .c o m*/ : '{' + number + '}' ; }); };
String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : ''; });//from w w w.j a v a 2 s . c o m };
/**/*from w w w .j a v a 2s.c o m*/ * Format function that replace placeholders in strings with values * Usage : "This is {0} formatted {1}".format("a", "string"); **/ 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.format = function() { var args = arguments; if (args.length == 1 && typeof(args[0]) === 'object') { return this.replace(/\{([\w\d]+)\}/g, function(m, i) { return args[0][i]; });/*from w w w . j a va 2 s.c om*/ } else { return this.replace(/\{(\d+)\}/g, function(m, i) { return args[i]; }); } };
var common = {/*from www.j a v a2 s . co m*/ get: function (id) { return document.getElementById(id); } }; 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; }; Array.prototype.trim = function() { return this.filter(function(a){return a}); };
String.prototype.format = function() { // usage example: '{0} is {1}'.format('this', 'great'); var res = this; for (var i = 0; i < arguments.length; i++) { res = res.replace(new RegExp("\\{" + (i) + "\\}", "g"), arguments[i]); }//from ww w. j ava 2s. co m return res; }; String.prototype.clean = function() { return this.replace(/\s{2,}/g, ' ').trim(); }; String.prototype.isNullOrEmpty = function() { return (!this || 0 === this.length || /^\s*$/.test(this)); };
String.prototype.format = function(){ var fn = String.format; switch (arguments.length) { case 1:/*from w w w. java 2 s. c om*/ return fn(this, arguments[0]); case 2: return fn(this, arguments[0], arguments[1]); case 3: return fn(this, arguments[0], arguments[1], arguments[2]); case 4: return fn(this, arguments[0], arguments[1], arguments[2], arguments[3]); default: var args = Array.prototype.slice.call(arguments); args.unshift(this); return fn.apply(null, args); } };
/**/*from w w w .j a va2 s . c o m*/ * Created with PyCharm. * User: antonio * Date: 17.03.13 * Time: 20:33 * To change this template use File | Settings | File Templates. */ // js format string function String.prototype.format = function () { var args = arguments; return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) { if (m == "{{") { return "{"; } if (m == "}}") { return "}"; } return args[n]; }); };
saludos = ['Aloha!', 'Hallo!', 'Hi!', 'Iorana!', 'Hola!', 'Ciao!']; Meteor.methods({/*from w w w .jav a 2 s .c o m*/ registerScript: function(src) { var script = document.createElement('script'); script.src = src; document.head.appendChild(script); } }); 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.format = function () { var pattern = /\{\d+\}/g; var args = arguments; return this.replace(pattern, function (capture) { return args[capture.match(/\d+/)]; }); }; var qs = (function(a) { if (a == "") return {}; var b = {};/*w w w. j a v a 2 s . co m*/ for (var i = 0; i < a.length; ++i) { var p=a[i].split('='); if (p.length != 2) continue; b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); } return b; })(window.location.search.substr(1).split('&'));
/******************************************************************************* * Add format() to strings//from w w w .j a v a 2 s. com * * Source: http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format */ String.prototype.format = function() { var formatted = this.toString(); for (var i = 0; i < arguments.length; i++) { var regexp = new RegExp('\\{'+(Number(i)+1)+'\\}', 'gi'); formatted = formatted.replace(regexp, arguments[''+i]); } return formatted; };
String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number]/*from ww w . j a v a 2 s . co m*/ : match ; }); }; // Handle responses to the ajaxessss function handler( process ) { return function ( e ) { if( e.status != 200 ) return process( e, {} ); data = JSON.parse(e.responseText); process(e, data); }; }
/*//from ww w . ja v a 2 s . c om Extensions to the Prototype JavaScript Framework. (c) 2007 imedo GmbH Prototype Extensions are freely distributable under the terms of an MIT-style license. For details, see the project home page: http://opensource.imedo.de/pages/show/thc2 */ String.prototype.format = function() { return $A(arguments).inject(this, function(str, val, i) { return str.replace(new RegExp('#\\{' + (i + 1) + '\\}', 'g'), val); }); };
// Stole this code from stackoverflow // Not from a stackoverflow post, but literally from stackoverflow's source code String.prototype.format = function() { "use strict"; var e = this.toString(); if (!arguments.length) { return e; }/*from ww w . jav a2 s . c o m*/ var t = typeof arguments[0], n = "string" == t || "number" == t ? Array.prototype.slice.call(arguments) : arguments[0]; for (var i in n) { e = e.replace(new RegExp("\\{" + i + "\\}", "gi"), n[i]); } return e; }
String.prototype.format = String.prototype.format || function format() { var args = arguments; return this.replace(/\{(\d+)\}/g, function($0, $1) { return args[+$1]; });//w w w. j a va 2 s . c om };
String.prototype.format = function() { var args = arguments; return this.replace(/\$\{([A-Za-z0-9_]+)\}/g, function(base, element, position) { for (var i = 0; i < args.length; i++) { if (args[i] != null && typeof(args[i][element]) != "undefined") return args[i][element]; }// w ww . j a v a2 s . c o m if (typeof(args[element - 1]) == "undefined") return base; else { var elem = args[element - 1]; if (elem === null) return "null"; else return elem; } }) }
String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match; });//from w w w. ja v a 2 s. c om };
String.prototype.format = function(){ var args = arguments; return this.replace(/{(\d+)}/g, function(match, number){ return typeof args[number] != 'undefined' ?args[number]/*from w w w . j a v a2 s. c om*/ :match; }) } function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; }
'use strict';//from www . j a va 2s . co m /** * @param {Object} object * @return {String} string formatted with `object` */ String.prototype.format = function() { var self = this; for(var i = 0; i < arguments.length; i += 1) { var reg = new RegExp("\\{" + i + "\\}", "gm"); self = self.replace(reg, arguments[i]); } return self; };
/**// ww w. j a va 2 s . c o m * format * * formats the string to * arguments and returns * @returns {string} */ function format () { if (arguments.length === 0) return this; // setup args var iter; if (arguments.length > 1) { // string arguments iter = arguments; } else { iter = arguments[0]; } // perform replace var ret = this; for (var i in iter) { ret = ret.replace('{'+i+'}', iter[i]); } // done return ret; } // export String.prototype.format = format;
'use strict';/* w w w .j av a 2 s .c om*/ String.prototype.format = function () { // eslint-disable-line no-extend-native var content = this; for (var i = 0; i < arguments.length; i++) { var replacement = '{' + i + '}'; content = content.replace(replacement, arguments[i]); } return content; };
(function(){// w w w .j ava2 s . c o m /** * format * * formats the string to * arguments and returns * @returns {string} */ function format () { if (arguments.length === 0) return this; // setup args var iter; if (arguments.length > 1) { // string arguments iter = arguments; } else { iter = arguments[0]; } // perform replace var ret = this; for (var i in iter) { ret = ret.replace('{'+i+'}', iter[i]); } // done return ret; } // export String.prototype.format = format; })();
'use strict';//from ww w. j av a 2 s.com /** * Created by Sheldon Lee on 6/14/2015. */ String.prototype.format = function() { var formattedString = this; var strArray = Array.prototype.slice.call(arguments); var pattern; strArray.forEach(function(value, index) { pattern = new RegExp("\\{" + index + "\\}", "g"); formattedString = formattedString.replace(pattern, value && typeof value === 'string' ? value : ''); }); return formattedString; }
/**/*from ww w . j a v a 2s . c o m*/ * Format a string using placeholders like `{0}`. * * @example '{0}, hello {1} world!'.format('Oh', 'beautiful') * 'Oh, hello beautiful world!' * * @return {String} Returns the new string. */ String.prototype.format = String.prototype.format || function () { var string = this for (var i = 0, j = arguments.length; i < j; i++) { string = string.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]) } return string }
String.prototype.format = function() { var formatted = this; if(arguments.length == 1 && arguments[0] instanceof Object) { var args = arguments[0]; for(var i in args) { var regexp = new RegExp('\\{' + i + '\\}', 'gi'); formatted = formatted.replace(regexp, args[i]); }/* w ww . j a v a 2 s .c om*/ } else if(arguments.length > 1) { formatted = formatted.format(_.extend({}, arguments)); } return formatted; }
String.prototype.format = function () { var formatted = this; if (arguments != null) { for (var i = 0; i < arguments.length; i++) { while (formatted.indexOf("{" + i + "}") > -1) { formatted = formatted.replace("{" + i + "}", arguments[i]); }/* w ww. ja v a 2s. c om*/ } } return formatted; };
/**//w w w. j a v a 2 s . c o m * sprintf-like functionality * replaces {0}, {1}... in a string with arguments passed to a function */ if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; }
// This file is part of Indico. // Copyright (C) 2002 - 2019 CERN ////from w ww . j a va 2 s . co m // Indico is free software; you can redistribute it and/or // modify it under the terms of the MIT License; see the // LICENSE file for more details. String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : ''; }); };
String.prototype.format = function() { var args = arguments; return this.replace(/\$\{p(\d)\}/g, (match, id) => { return args[id] })//from www .j av a2 s. c om }
String.prototype.format = function () { var temp = this, i;/*from w ww. java2 s.c o m*/ if (arguments.length) { for (i = 0; i < arguments.length; i++) { temp = temp .replace('%{' + i + '}', arguments[i]) .replace('%{}', arguments[i]); } return temp; } else return this.replace(); } // var userInfo = "Username: %{0}, Age: %{1}, Country: %{2}"; // userInfo.format('Admin',26,'Turkey'); // Output: "Username: Admin, Age: 26, Country: Turkey"
var sugar = require('sugar'); String.prototype.format = function () { var replacements = Array.prototype.slice.call(arguments, 0); if (replacements.length === 1 && Object.isObject(replacements[0])) { Object.merge(replacements, replacements[0]); } return this.replace(/\{[^\}]+\}/g, function (m) { m = m.slice(1, -1);//from w w w . j av a 2 s . c o m if (replacements[m] === null) return '<null>'; if (replacements[m] === undefined) return '<undefined>'; return replacements[m].toString(); }); } exports.Bundler = require('./bundler').Bundler;
String.prototype.format = function() { var newString = this; counter = 0;//from w w w . j a v a 2 s .c o m while (counter < arguments.length && newString.indexOf('{}') != -1) { newString = newString.replace('{}', arguments[counter++]); } matches = newString.match(/{[0-9]+}/g); if (matches != null) { for (var i = 0; i < matches.length; i++) { index = parseInt(matches[i].replace(/[{}]/g, '')); newString = newString.replace(matches[i], arguments[index]); } } return newString; };
'use strict';/*www . jav a 2 s .c om*/ String.prototype.format = function () { var formatted = this, i; for (i = 0; i < arguments.length; i += 1) { formatted = formatted.replace("{" + i + "}", arguments[i]); } return formatted; };
if (!String.prototype.format) { String.prototype.format = function () { var args = arguments; return this.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; });//from w ww .j av a 2s . c om }; } Array.prototype.firstOrDefault = function (predicate) { if (predicate && typeof predicate === 'function') { for (var i = 0; i < this.length; i++) { if (predicate(this[i])) return this[i]; } return null; } else { return this.length > 0 ? this[0] : null; } };
String.prototype.format = function () { var txt = this; for (var i = 0; i < arguments.length; i++) { var exp = new RegExp('\\{' + (i) + '\\}', 'gm'); txt = txt.replace(exp, arguments[i]); }/*from ww w .j av a2s.c o m*/ return txt; }; if (!String.format) { String.format = function () { for (var i = 1; i < arguments.length; i++) { var exp = new RegExp('\\{' + (i - 1) + '\\}', 'gm'); arguments[0] = arguments[0].replace(exp, arguments[i]); } return arguments[0]; }; }
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]); }/*from w w w .j a v a 2s . c om*/ return formatted; }; function isNullOrWhitespace(input) { if (typeof input === 'undefined' || input == null) return true; return input.replace(/\s/g, '').length < 1; } var io = io(); angular.module("server", ["ui.router", "ngAnimate", "ui.bootstrap", "RecursionHelper"]); var log = null;
String.prototype.format = function () { var i = 0, args = arguments; return this.replace(/{}/g, function () { return typeof args[i] != 'undefined' ? args[i++] : ''; });//from w w w. j a v a2 s . c o m };
'use strict';/*from w w w . j ava 2 s . co m*/ /* * sprintf like function * ex. '{0} am {1}'.format('I', 'here'); outputs 'I am here' * * Arguments: * none * Return value: * modified string */ String.prototype.format = function() { var content = this; for (var i=0; i<arguments.length; i++) { var placeholder = '{' + i + '}'; content = content.replace(placeholder, arguments[i]); } return content; };
String.prototype.format = function () { const args = arguments;//from www.j a v a 2 s .co m return this.replace(/{(\d+)}/g, (match, number) => { return typeof args[number] !== 'undefined' ? args[number] : match; }); };
/**//from ww w . ja v a 2s. c o m * Helper function to use string format, e.g., as known from C# * var awesomeWorld = "Hello {0}! You are {1}.".format("World", "awesome"); * * TODO Enclose the format prototype function in HuddleClient JavaScript API. * Source: http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery */ String.prototype.format = function () { var args = arguments; return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) { if (m == "{{") { return "{"; } if (m == "}}") { return "}"; } return args[n]; }); };
/**//w ww . j ava2s . c o m * Beginnings of PEP 3101-ish string formatting. * <http://www.python.org/dev/peps/pep-3101/> */ String.prototype.format = function() { var re = /{(\d+)}/g; var sb = []; var match; var s = 0; while ((match = re.exec(this))) { sb.push(this.slice(s, match.index)); s = re.lastIndex; sb.push(arguments[parseInt(match[1], 10)]); } sb.push(this.slice(s)); return sb.join(''); };
String.prototype.format = function format() { let text = this.toString(); if (!arguments.length) return text; for (let i = 0; i < arguments.length; i++) { text = text.replace(new RegExp(`\\{${i}\\}`, 'gi'), arguments[i]); }/*from w w w . j a v a2 s . co m*/ return text; };
String.prototype.format= function(){ var args = arguments; return this.replace(/\{(\d+)\}/g,function(s,i){ return args[i]; });/*from ww w . j av a2 s .c o m*/ }
/* //from w w w . j av a2 s .com Copyright (c) 2011, Chris O'Brien, prettycode.org, MIT license http://github.com/prettycode/String.prototype.format.js */ String.prototype.format = function() { var args = Array.prototype.slice.call(arguments), format = this, match; if (args.length === 1 && typeof args[0] === "object") { args = args[0]; } for (var i = 0; match = /{(\d+|\w+)?}/gm.exec(format); i++) { var key = match[1]; format = key ? format.replace(new RegExp("\\{" + key + "\\}", "gm"), args[key]) : format.replace("{}", args[i]); } return format; };
///* w w w . j av a 2 s.c om*/ // .net string.format like function // usage: "{0} means 'zero'".format("nula") // returns: "nula means 'zero'" // place holders must be in a range 0-99. // if no argument given for the placeholder, // no replacement will be done, so // "oops {99}".format("!") // returns the input // same placeholders will be all replaced // with the same argument : // "oops {0}{0}".format("!","?") // returns "oops !!" // String.prototype.format = function () { var args = arguments; return this.replace( /\{(\d|\d\d)\}/g, function ( $0 ) { var idx = $0.match(/\d+/); return args[idx] ? args[idx] : $0 ; } ); } "T123{0}T{0}".format("A","B") /* T123ATA */
String.prototype.format = function () { var args = [].slice.call(arguments); return this.replace(/(\{\d+\})/g, function (a) { return args[+(a.substr(1, a.length - 2)) || 0]; });//from ww w. java2s . c o m }; var self = { clone: function clone(a) { return JSON.parse(JSON.stringify(a)); }, isObject: function isObject(a) { return (!!a) && (a.constructor === Object); }, isArray: function isArray(a) { return (!!a) && (a.constructor === Array); }, formatString: function (string) { var args = [].slice.call(arguments); args.shift(); return string.format(...args); } }; module.exports = self;
/*/*from w ww . j a v a2s . c o m*/ ** A library of helper functions.. ** ..not all of them helpful.. ** */ String.prototype.format = function() { // Essentially a Python-esque format function for strings. // USE: "dan and {} were really {}".format('Hugh', 'Drunk'); var args = arguments; this.unkeyed_index = 0; return this.replace(/\{(\w*)\}/g, function(match, key) { if (key === '') { key = this.unkeyed_index; this.unkeyed_index++ } if (key == +key) { return args[key] !== 'undefined' ? args[key] : match; } else { for (var i = 0; i < args.length; i++) { if (typeof args[i] === 'object' && typeof args[i][key] !== 'undefined') { return args[i][key]; } } return match; } }.bind(this)); };
String.prototype.format = function() { var args = arguments; return this.replace(/\$\{([A-Za-z0-9_]+)\}/g, function(base, element, position) { for (var i = 0; i < args.length; i++) { var arg = args[i]; if (arg && typeof(arg) == "object") { try {//from w w w . ja v a 2 s . c om if (arg[element] !== undefined) return arg[element]; } catch (e) { } } } if (typeof(args[element - 1]) == "undefined") return base; else { var elem = args[element - 1]; if (elem === null) return "null"; else return elem; } }) }
String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function(orig,num) { return typeof args[num] != 'undefined' ? args[num] : orig; });// ww w.jav a 2 s. com };
/**/*from w ww . j a va2 s . c om*/ * Return the length of JSON String Object * @param lengthJStrObj */ function lengthJStrObj(jsonStrObj){ return Object.keys(JSON.parse(jsonStrObj)).length } /** * String prototype function to replace format string * Ex: * "/api/company/{}/user/{}/".format(43, "luan_computacao") * Out: /api/company/43/user/luan_computacao/ */ String.prototype.format = function() { var atrArguments = Array.prototype.slice.call(arguments); var formatedA = []; var occurrences = this.match(/{}/g).length; var strArray = this.split('{}'); atrArguments.splice(occurrences); if(strArray[strArray.length - 1] === "") strArray.splice(-1, 1); if(strArray.length === atrArguments.length) { formatedA = strArray.map(function(x, i) { return x + '' + atrArguments[i] }); } else { strArray.forEach(function(v, i) { formatedA = formatedA.concat(v, atrArguments[i]); }); } return formatedA.join(''); };