标签:
function stringformat() {
var args = Array.prototype.slice.call(arguments);
if (args.length == 0) { return ‘‘;}
if (args.length == 1) { return args[0]; }
var str = args.shift();
return str.replace(
/\\?\{([^{}]+)\}/g,
function(match, name) {
if (match.charAt(0) === ‘\\‘) {
return match.slice(1);
}
return (args[name] === undefined) ? match : args[name];
}
);
}
// 返回 my name is Rain Man and age is 28
stringformat(‘my name is {0} and age is {1}‘, ‘Rain Man‘, 28, ‘an‘);
JavaScript模仿C#中string.format效果
标签:
原文地址:http://www.cnblogs.com/rainman/p/4498776.html