最近在看一同事写的代码时,有一个字符串用了一堆"+"号,看了半天没明天到底会输出什么样的内容,就想到用字符串连接的类,把以前的方法写成了类的方式,方便调用。下面的类支持实例调用和静态调用,参数可以是单独的字符串,或者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. JQuery制作网页—— 第一章 JavaScript基础

    1. JavaScript(弱类型语言):是一种描述性语言,也是一种基于对象(Object)和事件驱动(Event Driven)的,并具有安全性能的脚本语言. 特点:1.主要用来在HTML页面中添加 ...

  2. Ajax上传文件/照片时报错TypeError :Illegal invocation

    问题 Ajax上传文件/照片时报错TypeError :Illegal invocation 解决 网上搜索问题,错误原因可能有以下几个,依次检查: 请求类型有误,如post请求,但在后台设置的是ge ...

  3. nop 插件解析

    在计算领域,插件( plug-in or plugin)是将特定的功能增加到大型软件中的软件组件. nopCommerce插件用来扩展nopCommerce的功能.nopCommerce 有几种插件. ...

  4. React路由-进阶篇

    路由进阶 1.多级路由,和之前的思想一样,在子路由里面继续写Route,继续挂载组件,就可以实现多级路由 比如这样:class Food extends Component{ render() { r ...

  5. ethereum(以太坊)(五)--Bool

    pragma solidity ^0.4.0; contract Bool{ uint num1 = 100; uint num2 = 200; bool _c = true; // &&am ...

  6. MySQL数据库 : 自关联,视图,事物,索引

    自关联查询(自身id关联自身id(主键),查询的时候可以逻辑分为两个表,然后分别起一个别名来区分) select * from areas as cityinner join areas as pro ...

  7. php-语言参考-基本语法3.1

    一,PHP代码的开始和结束标记 1,<?php 和 ?> //重点 2,<script language="php"> 和 </script> ...

  8. Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理

    E. Congruence Equation time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  9. TouTiao开源项目 分析笔记9 实现一个问答主页面

    1.根据API返回创建几个基础的Bean 1.1.WendaArticleDataBean类 API返回的数据如下: /** * cell_type : 36 * extra : {"wen ...

  10. css媒体类型

    all 用于所有的媒体设备. aural 用于语音和音频合成器. braille 用于盲人用点字法触觉回馈设备. embossed 用于分页的盲人用点字法打印机. handheld 用于小的手持的设备 ...