开发者

Do we have something like C# String.Format(...) in JavaScript? [duplicate]

开发者 https://www.devze.com 2023-03-21 11:12 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: JavaScript equivalent to print开发者_JS百科f/string.format
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

JavaScript equivalent to print开发者_JS百科f/string.format

Do we have something like C# String.Format(...) in JavaScript?

I like to be able to say String.Format('text text {0}, text text {1}', value1, value2);

and ideally as an extension method:

'text text {0}, text text {1}'.format(value1, value2);

Thanks,


here is your solution:

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

more informations here => Equivalent of String.format in jQuery

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号