Like Java, it is very resource-consuming to use the "+" sign in JS to piece together strings, so in the case of a large number of strings, we also need a tool similar to StringBuffer.
The following is to use the Array.join() method to implement StringBuffer
function StringBuffer() { this._strs = new Array; } StringBuffer.prototype.append = function (str) { this._strs.push(str); }; StringBuffer.prototype.toString = function() { return this._strs.join(""); };use:
var sb=new StringBuffer();sb.append("sss")sb.append("ddd");sb.toString(); //"sssddd"Attached the string formatting method used in a project
/*** Format string* format("{0},{1}","ddd","ffff");* format('<button id="{0}" type="{1}">{2}</button>',"btnOk","Button","Ok")**/function format(str){ for(var i=0;i<arguments.length-1;i++){ var placeHolder="{"+i+"}"; if(str.indexOf(placeHolder)!=-1){ str=str.replace(placeHolder,arguments[i+1]); } } return str;}The above simple example of JS pieced together strings is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.