为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.
有可能出现{{0}}的情况,替换结果应该为{str},请注意

解决方案 »

  1.   

    http://topic.csdn.net/u/20090813/12/9981cbce-47ab-4b9c-a97b-ac96166f2dfc.html
    前段时间刚好讨论过
      

  2.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    String.prototype.format = function(argu){
    var val = this;
    if(argu instanceof Array){
    for(var i=0; i<argu.length; i++){
    val = val.replace("{" + i + "}", argu[i]);
    }
    }
    return val;
    };
    var msg = "The value of {0} must be less than {1}.".format(['score', 100]);alert(msg);
    </script>
    </head>
    <body>
    </body>
    </html>