JS StringFormat函數


 

JS版StringFormat函數

原理:根據參數個數循環替換字符串 

        String.prototype.StringFormat = function() {
            if (arguments.length == 0) {
                return this;
            }
            for (var StringFormat_s = this, StringFormat_i = 0; StringFormat_i < arguments.length; StringFormat_i++) {
                StringFormat_s = StringFormat_s.replace(new RegExp("\\{" + StringFormat_i + "\\}", "g"), arguments[StringFormat_i]);
            }
            return StringFormat_s;
        };
        //用法 : var str = "{0}{1}".StringFormat("Eric", "Yu");
        // str = "EricYu"

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM