相当于 jQuery 中的 String.format
我正在尝试将一些 JavaScript 代码从 MicrosoftAjax 移动到 JQuery。 我在 MicrosoftAjax 中使用 JavaScript 中流行的 .net 方法的等效项,例如 String.format()、String.startsWith() 等。在 jQuery 中是否有与它们等效的项?
I'm trying to move some JavaScript code from MicrosoftAjax to JQuery. I use the JavaScript equivalents in MicrosoftAjax of the popular .net methods, e.g. String.format(), String.startsWith(), etc. Are there equivalents to them in jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
我无法得到乔什·斯托多拉(Josh Stodola)的答案,但以下内容对我有用。 请注意
prototype
的规范。 (在 IE、FF、Chrome 和 Safari 上测试。):s
确实应该是this
的克隆,以免造成破坏性方法,但其实没有必要。I couldn't get Josh Stodola's answer to work, but the following worked for me. Note the specification of
prototype
. (Tested on IE, FF, Chrome, and Safari.):s
really should be a clone ofthis
so as not to be a destructive method, but it's not really necessary.扩展 adamJLev 的精彩答案上面,这里是 TypeScript 版本:
Expanding on adamJLev's great answer above, here is the TypeScript version:
我有一个将其添加到字符串原型的插件:
字符串格式
它不仅像其他一些示例那么短,而且更加灵活。
用法与 c# 版本类似:
此外,还添加了对使用名称和名称的支持。 对象属性
I have a plunker that adds it to the string prototype:
string.format
It is not just as short as some of the other examples, but a lot more flexible.
Usage is similar to c# version:
Also, added support for using names & object properties
您还可以用这样的替换来关闭数组。
或者你可以尝试
绑定
You can also closure array with replacements like this.
or you can try
bind
使用函数式编程:
Using functional programming:
虽然不完全是 Q 所要求的,但我已经构建了一个类似的但使用命名占位符而不是编号的占位符。 我个人更喜欢使用命名参数,然后将一个对象作为参数发送给它(更详细,但更容易维护)。
这是一个示例用法...
Though not exactly what the Q was asking for, I've built one that is similar but uses named placeholders instead of numbered. I personally prefer having named arguments and just send in an object as an argument to it (more verbose, but easier to maintain).
Here's an example usage...
使用支持 EcmaScript 2015 (ES6) 的现代浏览器,您可以享受 模板字符串。 您可以直接将变量值注入其中,而不是格式化:
请注意,必须使用反引号(`)编写模板字符串。
Using a modern browser, which supports EcmaScript 2015 (ES6), you can enjoy Template Strings. Instead of formatting, you can directly inject the variable value into it:
Note the template string has to be written using back-ticks (`).
到目前为止提出的答案都没有对使用封装进行一次初始化并存储正则表达式以供后续使用进行明显的优化。
此外,如果已经存在,则所有示例都不尊重 format() 实现。
None of the answers presented so far has no obvious optimization of using enclosure to initialize once and store regular expressions, for subsequent usages.
Also, none of the examples respects format() implementation if one already exists.
已经过去了赛季末,但我一直在查看给出的答案,并且值得我花几便士:
用法:
方法:
结果:
Way past the late season but I've just been looking at the answers given and have my tuppence worth:
Usage:
Method:
Result:
这是我的:
不是防弹的,但如果你明智地使用它,它就会起作用。
Here's mine:
Not bullet proof but works if you use it sensibly.
这是我的版本,它能够转义“{”,并清理那些未分配的占位符。
Here's my version that is able to escape '{', and clean up those unassigned place holders.
以下答案可能是最有效的,但需要注意的是仅适用于参数的 1 对 1 映射。 这使用连接字符串的最快方法(类似于字符串生成器:字符串数组,连接)。 这是我自己的代码。 但可能需要一个更好的分离器。
像这样使用它:
The following answer is probably the most efficient but has the caveat of only being suitable for 1 to 1 mappings of arguments. This uses the fastest way of concatenating strings (similar to a stringbuilder: array of strings, joined). This is my own code. Probably needs a better separator though.
Use it like:
这违反了 DRY 原则,但这是一个简洁的解决方案:
This violates DRY principle, but it's a concise solution:
现在您可以使用模板文字:
Now you can use Template Literals:
这是 Josh 发布的函数的更快/更简单(和原型)的变体:
用法:
我经常使用它,所以我将它别名为
f
,但您也可以使用更详细的格式。 例如
'Hello {0}!'.format(name)
This is a faster/simpler (and prototypical) variation of the function that Josh posted:
Usage:
I use this so much that I aliased it to just
f
, but you can also use the more verboseformat
. e.g.'Hello {0}!'.format(name)
上面的许多函数(除了 Julian Jelfs 的函数)都包含以下错误:
或者,对于从参数列表末尾向后计数的变体:
这是一个正确的函数。 这是 Julian Jelfs 代码的原型变体,我把它做得更紧了:
这是一个稍微更高级的版本,它允许您通过加倍大括号来转义大括号:
这可以正常工作:
这是 Blair Mitchelmore 的另一个很好的实现,具有许多不错的额外功能: https: //web.archive.org/web/20120315214858/http://blairmitchelmore.com/javascript/string.format
Many of the above functions (except Julian Jelfs's) contain the following error:
Or, for the variants that count backwards from the end of the argument list:
Here's a correct function. It's a prototypal variant of Julian Jelfs's code, which I made a bit tighter:
And here is a slightly more advanced version of the same, which allows you to escape braces by doubling them:
This works correctly:
Here is another good implementation by Blair Mitchelmore, with a bunch of nice extra features: https://web.archive.org/web/20120315214858/http://blairmitchelmore.com/javascript/string.format
创建一个格式函数,它接受集合或数组作为参数
用法:
代码:
Made a format function that takes either a collection or an array as arguments
Usage:
Code:
有一个(某种程度上)官方选项:jQuery.validator.format。
附带 jQuery 验证插件 1.6(至少)。
与 .NET 中的
String.Format
非常相似。编辑修复了损坏的链接。
There is an (somewhat) official option: jQuery.validator.format.
Comes with jQuery Validation Plugin 1.6 (at least).
Quite similar to the
String.Format
found in .NET.Edit Fixed broken link.
如果您使用验证插件,可以使用:
jQuery.validator.format("{0} {1}", "cool", "formatting") = 'cool Formatting'
http://docs.jquery.com/Plugins/Validation/jQuery.validator.format#templateargumentargumentN ...
If you're using the validation plugin you can use:
jQuery.validator.format("{0} {1}", "cool", "formatting") = 'cool formatting'
http://docs.jquery.com/Plugins/Validation/jQuery.validator.format#templateargumentargumentN...
ASP.NET AJAX 的源代码可供您参考,因此您可以从中挑选并将您想要继续使用的部分包含到单独的 JS 文件中。 或者,您可以将它们移植到 jQuery。
这是格式函数...
这是endsWith 和startsWith 原型函数...
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...
And here are the endsWith and startsWith prototype functions...