String.format در جاوا اسکریپت
نویسنده: یوسف نژاد
تاریخ: ۱۳۹۲/۰۳/۱۹ ۱۱:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
String.format = function () {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
s = s.replace("{" + i + "}", arguments[i + 1]);
}
return s;
};String.format = function () {
var s = arguments[0];
for (var arg in arguments) {
var i = parseInt(arg);
s = s.replace("{" + i + "}", arguments[i + 1]);
}
return s;
};console.log(String.format("{0} is nice!", "donettips.info"));donettips.info is nice!
console.log(String.format("{0} is {1} nice! {0} is {1} nice!", "donettips.info", "very"));donettips.info is very nice! {0} is {1} nice!String.format = function () {
var original = arguments[0],
replaced;
for (var i = 0; i < arguments.length - 1; i++) {
replaced = '';
while (replaced != original) {
original = replaced || original;
replaced = original.replace("{" + i + "}", arguments[i + 1]);
}
}
return replaced;
};donettips.info is very nice! donettips.info is very nice!
String.format = function () {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i + 1]);
}
return s;
};String.format = function () {
var s = arguments[0],
i = arguments.length - 1;
while (i--) {
s = s.replace(new RegExp('\\{' + i + '\\}', 'g'), arguments[i + 1]);
}
return s;
};console.log(String.format("{0}:0 {1}:1 {2}:2", "zero", "{2}", "two"));zero:0 {2}:1 two:2zero:0 two:1 two:2
console.log(String.format("{0}:0 {1}:1 {2}:2", "zero", "one", "{1}"));zero:0 one:1 one:2
zero:0 one:1 {1}:2String.format = function () {
var args = arguments;
return args[0].replace(/{(\d+)}/g, function (match, number) { return args[parseInt(number) + 1]; });
};console.log(String.format("{0} is {1} nice!", "donettips.info"));donettips.info is undefined nice!
String.format = function () {
var s = arguments[0],
args = arguments;
return s.replace(/{(\d+)}/g, function (match, number) {
var i = parseInt(number);
return typeof args[i + 1] != 'undefined' ? args[i + 1] : match;
});
};console.log(String.format("{0}:0 {1}:1 {2}:2, {{0}} {{{1}}} {{{{2}}}} {2}", "zero", "{2}", "two"));zero:0 {2}:1 two:2, {zero} {{{2}}} {{{two}}} twoString.format = function () {
var s = arguments[0],
args = arguments;
return s.replace(/\{\{|\}\}|\{(\d+)\}/g, function (match, number) {
if (match == "{{") { return "{"; }
if (match == "}}") { return "}"; }
var i = parseInt(number);
return typeof args[i + 1] != 'undefined'
? args[i + 1]
: match;
});
};zero:0 {2}:1 two:2, {0} {{2}} {{2}} twoString.prototype.format = function () {
...
}String.prototype.format = function () {
var s = this.toString(),
args = arguments;
return s.replace(/\{\{|\}\}|\{(\d+)\}/g, function (match, number) {
if (match == "{{") { return "{"; }
if (match == "}}") { return "}"; }
return typeof args[number] != 'undefined'
? args[number]
: match;
});
};console.log("{0}:0 {1}:1 {2}:2, {{0}} {{{1}}} {{{{2}}}} {2}".format("zero", "{2}", "two"));String.format = function () {
var s = arguments[0],
args = arguments[1];
for (var arg in args) {
s = s.replace(new RegExp("{" + arg + "}", "g"), args[arg]);
}
return s;
};String.prototype.format = function () {
var s = this.toString(),
args = arguments[0];
for (var arg in args) {
s = s.replace(new RegExp("{" + arg + "}", "g"), args[arg]);
}
return s;
};console.log(String.format("{site} is {adj}! {site} is {adj}!", { site: "donettips.info", adj: "nice" }));
console.log("{site} is {adj}! {site} is {adj}!".format({ site: "donettips.info", adj: "nice" }));String.format = function String$format(format, args) {
/// <summary locid="M:J#String.format" />
/// <param name="format" type="String"></param>
/// <param name="args" parameterArray="true" mayBeNull="true"></param>
/// <returns type="String"></returns>
// var e = Function._validateParams(arguments, [
// { name: "format", type: String },
// { name: "args", mayBeNull: true, parameterArray: true }
// ]);
// if (e) throw e;
return String._toFormattedString(false, arguments);
};
String._toFormattedString = function String$_toFormattedString(useLocale, args) {
var result = '';
var format = args[0];
for (var i = 0; ; ) {
var open = format.indexOf('{', i);
var close = format.indexOf('}', i);
if ((open < 0) && (close < 0)) {
result += format.slice(i);
break;
}
if ((close > 0) && ((close < open) || (open < 0))) {
if (format.charAt(close + 1) !== '}') {
throw Error.argument('format', Sys.Res.stringFormatBraceMismatch);
}
result += format.slice(i, close + 1);
i = close + 2;
continue;
}
result += format.slice(i, open);
i = open + 1;
if (format.charAt(i) === '{') {
result += '{';
i++;
continue;
}
if (close < 0) throw Error.argument('format', Sys.Res.stringFormatBraceMismatch);
var brace = format.substring(i, close);
var colonIndex = brace.indexOf(':');
var argNumber = parseInt((colonIndex < 0) ? brace : brace.substring(0, colonIndex), 10) + 1;
if (isNaN(argNumber)) throw Error.argument('format', Sys.Res.stringFormatInvalid);
var argFormat = (colonIndex < 0) ? '' : brace.substring(colonIndex + 1);
var arg = args[argNumber];
if (typeof (arg) === "undefined" || arg === null) {
arg = '';
}
if (arg.toFormattedString) {
result += arg.toFormattedString(argFormat);
}
else if (useLocale && arg.localeFormat) {
result += arg.localeFormat(argFormat);
}
else if (arg.format) {
result += arg.format(argFormat);
}
else
result += arg.toString();
i = close + 1;
}
return result;
}console.log(String.format("{0:n}, {0:c}, {0:p}, {0:d}", 100.0001));
// result: 100.00, ¤100.00, 10,000.01 %, 100.0001
console.log(String.format("{0:d}, {0:t}", new Date(2015, 1, 1, 10, 45)));
// result: 02/01/2015, 10:45var template = jQuery.validator.format("{0} is not a valid value");
console.log(template("abc"));
// result: 'abc is not a valid value'String.format([full format string], [arguments...]); // or: [date|number].format([partial format string]);
// Object path
String.format("Welcome back, {username}!",
{ id: 3, username: "JohnDoe" });
// Result: "Welcome back, JohnDoe!"
// Date/time formatting
String.format("The time is now {0:t}.",
new Date(2009, 5, 1, 13, 22));
// Result: "The time is now 01:22 PM."
// Date/time formatting (without using a full format string)
var d = new Date();
d.format("hh:mm:ss tt");
// Result: "02:28:06 PM"
// Custom number format string
String.format("Please call me at {0:+##0 (0) 000-00 00}.", 4601111111);
// Result: "Please call me at +46 (0) 111-11 11."
// Another custom number format string
String.format("The last year result was {0:+$#,0.00;-$#,0.00;0}.", -5543.346);
// Result: "The last year result was -$5,543.35."
// Alignment
String.format("|{0,10:PI=0.00}|", Math.PI);
// Result: "| PI=3.14|"
// Rounding
String.format("1/3 ~ {0:0.00}", 1/3);
// Result: "1/3 ~ 0.33"
// Boolean values
String.format("{0:true;;false}", 0);
// Result: "false"
// Explicitly specified localization
// (note that you have to include the .js file for used cultures)
msf.setCulture("en-US");
String.format("{0:#,0.0}", 3641.667);
// Result: "3,641.7"
msf.setCulture("sv-SE");
String.format("{0:#,0.0}", 3641.667);
// Result: "3 641,7"//inline arguments
String.format("some string with {0} and {1} injected using argument {{number}}", 'first value', 'second value');
//returns: 'some string with first value and second value injected argument {number}'
//single array
String.format("some string with {0} and {1} injected using array {{number}}", [ 'first value', 'second value' ]);
//returns: 'some string with first value and second value injected using array {number}'
//single object
String.format("some string with {first} and {second} value injected using {{propertyName}}",{first:'first value',second:'second value'});
//returns: 'some string with first value and second value injected using {propertyName}'