为 javascript 的 String 对象增加一个 format(args[])方法。 
此方法可以将字符串中的{0},{1},{2}等占位符替换为参数传入的值。 
 
如: var msg = 'The value of {0} must be less than {1}.'.format(['score', '100']); 
msg 的值为 The value of score must be less than 100. 
 

解决方案 »

  1.   

    String.prototype.format = function(/*, ...*/) {
    var arg = arguments;
    return this.replace(/\{(\d+)\}/g, function() {
    return arg[+arguments[1]];
    });
    }var msg = 'The value of {0} must be less than {1}.'.format(['score', '100']); 
    alert(msg);
      

  2.   

    传参的时候不用“[]”,否则就是一个数组参数。
    var msg = 'The value of {0} must be less than {1}.'.format('score', '100'); 
      

  3.   

    #2楼 得分:0回复于:2010-03-16 11:54:29
    #3楼 得分:0回复于:2010-03-16 12:54:26bug
      

  4.   

    LZ可能“public static void main(String args[]){}”的干活,不适应弱语言的说,lol。