收藏一个JavaScript字符串连接方法
最近在看一同事写的代码时,有一个字符串用了一堆"+"号,看了半天没明天到底会输出什么样的内容,就想到用字符串连接的类,把以前的方法写成了类的方式,方便调用。下面的类支持实例调用和静态调用,参数可以是单独的字符串,或者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字符串连接方法的更多相关文章
- step_by_step_记录一个javascript字符串处理问题
记录一个javascript字符串处理的问题 这一天下班,技术QQ群里的大神提出了一个问题,带着问题去思考. ? '---9890.999008-555555-55555555----' 对于这样的字 ...
- c++字符串连接方法大观
c++字符串连接方法大观 抛砖引玉: springf(config_itemID[i],"ItemID%s_%d",i,i); 大家说说自己都用什么方法,个有什么利弊呢?
- python字符串连接方法效率比较
方法1:直接通过加号(+)操作符连接 1 website = 'python' + 'tab' + '.com' 方法2:join方法 1 2 listStr = ['python', 'tab', ...
- Python学习笔记--Python字符串连接方法总结
声明: 这些总结的学习笔记,一部分是自己在工作学习中总结,一部分是收集网络中的知识点总结而成的,但不到原文链接.如果有侵权,请知会,多谢. python中有很多字符串连接方式,总结一下: 1)最原始的 ...
- javascript字符串处理方法
字符串处理方法 1.字符串合并操作:“ + ”2.parseInt() 将数字字符串转化为整数3.parseFloat() 将数字字符串转化为小数4.split() 把一个字符串分隔成字符串组成的数组 ...
- JavaScript字符串分割方法
使用split('')方法.此方法与Java的字符串分割方法方法名一样.
- Oracle数据库字符串连接方法
转至:http://database.51cto.com/art/201011/232267.htm 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLSe ...
- javascript字符串基本方法
1)auchor anchor() 方法用于创建 HTML 锚. var txt="Hello world!" document.write(txt.anchor("my ...
- 案例1:写一个压缩字符串的方法,例如aaaabbcxxx,则输出a4b2c1x3。
public static String zipString(String str){ String result = "";//用于拼接新串的变量 char last = str ...
随机推荐
- UVA_1434_YAPTCHA
The math department has been having problems lately. Due to immense amount of unsolicited automated ...
- idea中使用逆向工程----三部曲
逆向工程小伙伴可能都知道,可以根据公司大佬的数据库简单创建实体类和dao接口以及mapper的映射文件,逆向工程可能在数据库字段比较少的时候体现不会方便,但是当参与到数据库字段比较多的时候,我们不可能 ...
- Zeppelin interperter 模式设置总结图解1
如有错漏,望请指正,不胜感激. 参考:[zeppelin官网]:https://zeppelin.apache.org/docs/latest/interpreter/spark.html#inter ...
- Go单元测试与基准测试
Go单元测试 Go单元测试框架,遵循规则整理如下: 1.文件命名规则: 含有单元测试代码的go文件必须以_test.go结尾,Go语言测试工具只认符合这个规则的文件 单元测试文件名_test.go前面 ...
- linux下csv导出文件中文乱码问题
近日在服务器端通过导出csv文件,将数据从linux服务器端保存到windows桌面端,以便用户可以通过excel打开使用数据. 但是在使用excel打开csv文件时,出现了中文乱码的情况,但是使用记 ...
- oracle之bitmap索引
oracle常见的索引是BTree索引和Bitmap索引. BTree索引特点: 默认索引 适合大量增删改查 不能用or操作符 适合高基数的列(即唯一值多) 创建sql:create index li ...
- python IDLE中如何执行for、while、if等多行语句
>>> f=open("E:/pythonLearn/140.txt") >>> for line in f.readlines(): prin ...
- The Road to learn React书籍学习笔记(第一章)
react灵活的生态圈 Small Application Boilerplate: create-react-app Utility: JavaScript ES6 and beyond Styli ...
- Android 创建 SO 文件
创建工程,新建一个类,该类需要有一个static初始化块中调用System.loadLibrary("${soName}"),还需要有用native修饰的方法声明(无需实现),一个 ...
- python字典的整理信息
字典的增删改查大纲 增: dic={'age':18,'name':'liu','sex':'male'} dic['high'] = 185 #没有键值对,添加 dic['age'] = 16 #有 ...