Is it possible to call a C#-like String.Format() function in JQuery?
-
Are you use the ASP.NET MVC or Web Forms?Sergey K– Sergey K2011-05-10 20:58:29 +00:00Commented May 10, 2011 at 20:58
-
look at this question Equivalent of String.format in JQuerySergey K– Sergey K2011-05-10 21:00:33 +00:00Commented May 10, 2011 at 21:00
-
@Serghei I'd like to apply the best of each technology around my works. ThanksNam G VU– Nam G VU2011-05-20 14:28:35 +00:00Commented May 20, 2011 at 14:28
Add a comment
|
2 Answers
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; }
1 Comment
DeclanMcD
Had to change a couple of lines otherwise it malfunctioned when one of the arguments was null: var replacement = arguments[i + 1]; s = s.replace(reg, replacement == null ? "" : replacement);
Checkout format() that's part of the validation plugin that does C# like string formatting.
2 Comments
Farinha
+1 because although it's not purely javascript, it's a better option than a custom script if one is already using jquery.validate