最近在看一同事写的代码时,有一个字符串用了一堆"+"号,看了半天没明天到底会输出什么样的内容,就想到用字符串连接的类,把以前的方法写成了类的方式,方便调用。下面的类支持实例调用和静态调用,参数可以是单独的字符串,或者json的格式,或者类似参数数组的方式,见下面示例:

/**
* @class String concat
* @return {StrBuf/String}
* @constructor
* eg:
var buf = new StrBuf("contructor str\n");
buf.push("hello,")
.push("Today is {0}, {1}", "Monday", "March 28th")
.push("${name} is a good ${category} company", {name: "Google", category: "Intenet"});
document.write(buf);// auto call toString method
console.log(buf);
console.log(StrBuf("static {0} method", "invoke"));
*/
var StrBuf = function(s)
{
this.data = [];
if(s)
{
var args = arguments, buf;
if(this instanceof StrBuf)
{
this.push.apply(this, args);
}
else
{
// static invoke
buf = new StrBuf();
return buf.push.apply(buf, args).toString();
}
}
};
StrBuf.prototype = {
// add String to the instance
push: function(s, j) {
var args = arguments;
if(args.length < 2) {
this.data.push(s || "");
}
else if(typeof j == 'object')
{
this.data.push(s.replace(/\$\{([\w.]+)\}/g, function($, $1)
{
return ($1 in j) ? j[$1] : $;
}));
}
else
{
this.data.push(s.replace(/\{(\d+)\}/g, function($, $1) {
return args[+$1 + 1];
}));
}
return this;
},
toString: function() {
return this.data.join("");
}
};

调用示例如下:

	var buf = new StrBuf("contructor str\n");
buf.push("hello,");
buf.push("Today is {0}, {1}", "Monday", "March 28th");
buf.push("${name} is a good ${category} company", {name: "Google", category: "Intenet"});
document.write(buf);// auto call toString method
console.log(buf);
console.log(StrBuf("static {0} method", "invoke"));

收藏一个JavaScript字符串连接方法的更多相关文章

  1. step_by_step_记录一个javascript字符串处理问题

    记录一个javascript字符串处理的问题 这一天下班,技术QQ群里的大神提出了一个问题,带着问题去思考. ? '---9890.999008-555555-55555555----' 对于这样的字 ...

  2. c++字符串连接方法大观

    c++字符串连接方法大观 抛砖引玉: springf(config_itemID[i],"ItemID%s_%d",i,i); 大家说说自己都用什么方法,个有什么利弊呢?

  3. python字符串连接方法效率比较

    方法1:直接通过加号(+)操作符连接 1 website = 'python' + 'tab' + '.com' 方法2:join方法 1 2 listStr = ['python', 'tab',  ...

  4. Python学习笔记--Python字符串连接方法总结

    声明: 这些总结的学习笔记,一部分是自己在工作学习中总结,一部分是收集网络中的知识点总结而成的,但不到原文链接.如果有侵权,请知会,多谢. python中有很多字符串连接方式,总结一下: 1)最原始的 ...

  5. javascript字符串处理方法

    字符串处理方法 1.字符串合并操作:“ + ”2.parseInt() 将数字字符串转化为整数3.parseFloat() 将数字字符串转化为小数4.split() 把一个字符串分隔成字符串组成的数组 ...

  6. JavaScript字符串分割方法

    使用split('')方法.此方法与Java的字符串分割方法方法名一样.

  7. Oracle数据库字符串连接方法

    转至:http://database.51cto.com/art/201011/232267.htm 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLSe ...

  8. javascript字符串基本方法

    1)auchor anchor() 方法用于创建 HTML 锚. var txt="Hello world!" document.write(txt.anchor("my ...

  9. 案例1:写一个压缩字符串的方法,例如aaaabbcxxx,则输出a4b2c1x3。

    public static String zipString(String str){ String result = "";//用于拼接新串的变量 char last = str ...

随机推荐

  1. web worker,SSE,WebSocket,AJAX 与后端交互的方式

    一 web worker web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能.您可以继续做任何愿意做的事情:点击.选取内容等等,而此时 web worker ...

  2. 45.VUE学习之--组件之父组件使用scope定义子组件模板样式结构实例讲解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Visual Stutio 2015激活密钥

    Visual Stutio 2015 专业版激活密钥:HMGNV-WCYXV-X7G9W-YCX63-B98R2 Visual Stutio 2015 企业版激活密钥:HM6NR-QXX7C-DFW2 ...

  4. POJ 3484 二分

    Showstopper Description Data-mining huge data sets can be a painful and long lasting process if we a ...

  5. POJ:3616-Milking Time

    Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12324 Accepted: 5221 Descrip ...

  6. SAPバリアント

    SAPバリアント   VARI バリアント VARID バリアント一覧 VARIT バリアントテキスト VARIS バリアント割当 TVARV バリアント変数(クライアント非依存) TVARVC バリ ...

  7. hihocoder1014 : Trie树

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

  8. python datetime offset-aware与offset-navie相互转换

    python datetime offset-aware与offset-navie相互转换 2016年11月13日 16:20:43 阅读数:2393 有时,我们使用python 的datetime模 ...

  9. Java的接口和抽象类深入理解

    对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者确实有很多相似的地方,看了一整天别人怎么说,大致总结如下: 一.抽象类 在了解抽象类 ...

  10. 腾讯装扮下拉选项卡特效(QQ空间)

    <DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" ...