---
title: "JS版StringFormat函数"
author: "hackpro"
date: "2015-02-03T15:06:43Z"
tags: []
categories: ["互联网"]
slug: "js-stringformat"
draft: false
lastmod: "2015-02-03T15:06:43Z"
id: 2182982
url: "https://hackpro.iteye.com/blog/2182982"
---

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);
    }
    return StringFormat_s;
};
//用法 ： var str = "{0}{1}".StringFormat("Eric", "Yu");
// str = "EricYu"