现在的位置: 首页 > 综合 > 正文

模仿C#中的String.Format功能

2012年12月23日 ⁄ 综合 ⁄ 共 625字 ⁄ 字号 评论关闭

 

<script type="text/javascript">
            
var str ;
            String.prototype.template 
= function()
            {
                
var args = arguments;                
                
return this.replace(/\{(\d+)\}/g,function(m,i)
                                                    {
                                                        
return args[i];
                                                    }
                                     );
            };
            
            
var a = "I Love {0}, and You Love {1},Where are {0}!";
            alert(a.template(
"You","Me"));
            
        
</script>

以上代码参考网络资源,只供学习

抱歉!评论已关闭.