相当于jQuery中的String.format [英] Equivalent of String.format in jQuery

查看:163
本文介绍了相当于jQuery中的String.format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些JavaScript代码从MicrosoftAjax移动到JQuery。我使用流行的.net方法的MicrosoftAjax中的JavaScript等价物,例如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?

推荐答案

源代码ASP.NET AJAX可用供您参考,因此您可以选择它并将要继续使用的部分包含在单独的JS文件中。或者,你可以将它们移植到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.

这是格式函数...

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;
}

这里是endsWith和startsWith原型函数......

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);
}

这篇关于相当于jQuery中的String.format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆