JS类似Java String.format的函数

来源:互联网 发布:ug复杂模具编程思路 编辑:程序博客网 时间:2024/05/28 05:13
From: http://chenzenghua.iteye.com/blog/1770791

Js代码  收藏代码
  1. String.prototype.format = String.prototype.f = function () {  
  2.     var s = this,  
  3.         i = arguments.length;  
  4.   
  5.     while (i--) {  
  6.         s = s.replace(new RegExp('\\{' + i + '\\}''gm'), arguments[i]);  
  7.     }  
  8.     return s;  
  9. };  


使用: 
Js代码  收藏代码
  1. "chen {0} hua".format("zeng")  


结果: 
Js代码  收藏代码
  1. "chen zeng hua"  
0 0