[优化]JavaScript 格式化带有占位符字符串
2012-08-03 20:38
555 查看
/** * Format a message with placeholder. * * Examples: * format("X{0}Y{1}Z{2}") : XYZ * format("X{0}Y{1}Z{2}", '1') : X1YZ * format("X{0}Y{1}Z{2}", '1', '2') : X1Y2Z * format("X{0}Y{1}Z{2}", '1', '2', '3') : X1Y2Z3 * format("X{0}Y{1}Z{2}", '1', '2', '3', '4') : X1Y2Z3 * ------------------------------------ * format() : null * format("X{0}Y{1}Z{2}", null) : XYZ * format(null, '1') : null * ------------------------------------ * format("{0{0}1{1}2{2}}") : {012} * format("{0{0}1{1}2{2}}", 'x') : {0x12} * format("{0{0}1{1}2{2}}", 'x', 'y') : {0x1y2} * format("{0{0}1{1}2{2}}", 'x', 'y', 'z') : {0x1y2z} * * @Author http://blog.csdn.net/xxd851116 */ function format( message ) { if (!message) return null; var ss = message.split(/\{\d+?\}/); for ( var i = 0; i < ss.length; i++ ) { if (!arguments[i + 1]) break; ss[i] += arguments[i + 1]; } return ss.join(""); }
测试用例:
document.writeln("format(\"X{0}Y{1}Z{2}\") : " + format("X{0}Y{1}Z{2}") + "<br/>"); document.writeln("format(\"X{0}Y{1}Z{2}\", '1') : " + format("X{0}Y{1}Z{2}", '1') + "<br/>"); document.writeln("format(\"X{0}Y{1}Z{2}\", '1', '2') : " + format("X{0}Y{1}Z{2}", '1', '2') + "<br/>"); document.writeln("format(\"X{0}Y{1}Z{2}\", '1', '2', '3') : " + format("X{0}Y{1}Z{2}", '1', '2', '3') + "<br/>"); document.writeln("format(\"X{0}Y{1}Z{2}\", '1', '2', '3', '4') : " + format("X{0}Y{1}Z{2}", '1', '2', '3', '4') + "<br/>"); document.writeln("<hr />"); document.writeln("format() : " + format() + "<br/>"); document.writeln("format(\"X{0}Y{1}Z{2}\", null) : " + format("X{0}Y{1}Z{2}", null) + "<br/>"); document.writeln("format(null, '1') : " + format(null, '1') + "<br/>"); document.writeln("<hr />"); document.writeln("format(\"{0{0}1{1}2{2}}\") : " + format("{0{0}1{1}2{2}}") + "<br/>"); document.writeln("format(\"{0{0}1{1}2{2}}\", 'x') : " + format("{0{0}1{1}2{2}}", 'x') + "<br/>"); document.writeln("format(\"{0{0}1{1}2{2}}\", 'x', 'y') : " + format("{0{0}1{1}2{2}}", 'x', 'y') + "<br/>"); document.writeln("format(\"{0{0}1{1}2{2}}\", 'x', 'y', 'z') : " + format("{0{0}1{1}2{2}}", 'x', 'y', 'z') + "<br/>");
上面代码存在参数顺序的bug,感谢KimSoft提出的优化方案:/article/2799238.html
String.prototype.format = 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}! {4}"; alert(a.format("You","Me"));
相关文章推荐
- [优化]JavaScript 格式化带有占位符字符串
- javascript | jquery format格式化字符串
- JavaScript 实现格式化字符串函数String.format (解决引号嵌套转义符问题)
- 字符串格式化的JavaScript实现
- javascript日期对象格式化为字符串的实现方法
- lua程序性能优化(字符串连接与格式化)
- javascript模拟C#格式化字符串
- StringFormatter,一个javascript编写的字符串格式化库
- JavaScript_学习 6_字符串处理,String对象的字符串格式化方法
- javascript中字符串累加的性能优化(String和StringBuffer)
- JavaScript 字符串连接性能优化
- C#从文本文件(*.txt)中读取带有汉字的格式化的字符串
- Javascript将字符串日期格式化为yyyy-mm-dd的方法 js number 类型 没有length 属性 string类型才有
- JavaScript实现字符串与日期的互相转换及日期的格式化
- 高性能JavaScript--字符串加(+)和加等(+=)操作符优化方法
- JavaScript:获取系统当前时间,构造格式化的字符串
- 把数字格式化成带有分号的字符串
- PHP输出javascript脚本,带php参数,函数传参字符串并带有变量
- javascript模拟C#格式化字符串
- 编写高质量代码改善C#程序的157个建议[为类型输出格式化字符串、实现浅拷贝和深拷贝、用dynamic来优化反射]