Equivalent of String.format in jQuery
The source code for ASP.NET AJAX is available for your reference, so you can pick through it and include the parts you want to continue using into a separate JS file. Or, you can port them to jQuery.
Here is the format function...
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
And here are the endsWith and startsWith prototype functions...
String.prototype.endsWith = function (suffix) {
return (this.substr(this.length - suffix.length) === suffix);
}
String.prototype.startsWith = function(prefix) {
return (this.substr(0, prefix.length) === prefix);
}
C#-like String.Format() function in JQuery?
Equivalent of String.format in JQuery
Here is the format function...
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
JavaScript equivalent to printf/String.Format
Current JavaScript
From ES6 on you could use template strings:
let soMany = 10;
console.log(`This is ${soMany} times easier!`);
// "This is 10 times easier!"
See Kim's answer below for details.
Older answer
Try sprintf() for JavaScript.
If you really want to do a simple format method on your own, don’t do the replacements successively but do them simultaneously.
Because most of the other proposals that are mentioned fail when a replace string of previous replacement does also contain a format sequence like this:
"{0}{1}".format("{1}", "{0}")
Normally you would expect the output to be {1}{0}
but the actual output is {1}{1}
. So do a simultaneous replacement instead like in fearphage’s suggestion.
Is there a C# String.Format() equivalent in JavaScript?
Try sprintf() for javascript.
Or
// First, checks if it isn't implemented yet.
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
;
});
};
}
"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")
Both answers pulled from JavaScript equivalent to printf/string.format
Jquery string format issue with {0}
This isn't an error related to jQuery - it's the fact that you're trying to run a string instance's method that doesn't exist. A quick search gives me another StackOverflow answer, which happily implements String#format
for you.
String.prototype.format = function() {
var str = this;
for (var i = 0; i < arguments.length; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
str = str.replace(reg, arguments[i]);
}
return str;
}
Just paste that at the top or near to the top of your script file and use your newly-created function like "the {0} jumps over the {1}".format("quick brown fox", "lazy dog");
in the script.
.net's string.format() in javascript
There is no native solution, but here is one of many partial solutions: http://monocleglobe.wordpress.com/2010/01/12/everybody-needs-a-little-printf-in-their-javascript/
Are there any string formatting functions built into jQuery?
function format(formatString) {
var args = Array.prototype.slice.call(arguments,1);
return formatString.replace(/\{(\d+)\}/g, function(match, num) {
return args[Number(num)];
});
}
format("<li>Elapsed: {0}</li>\n<li>Display: {1}</li>\n<li>Total: {2}</li>",
ajaxElapsed, tableElapsed, ajaxElapsed + tableElapsed);
Related Topics
How to Decide When to Use Node.Js
Loading an Angularjs Controller Dynamically
Selected Text Event Trigger in JavaScript
What Is the Instanceof Operator in JavaScript
Best Way to Add Page Specific JavaScript in a Rails 3 App
How to Filter (Key, Value) with Ng-Repeat in Angularjs
Jquery.Inarray(), How to Use It Right
Set Value of Input Using JavaScript Function
How to Guarantee That My Enums Definition Doesn't Change in JavaScript
Make Several Requests to an API That Can Only Handle 20 Request a Minute
JavaScript Print Iframe Contents Only
What Is the Motivation for Bringing Symbols to Es6