String concatenation is often encountered in JavaScript, but it can be cumbersome if the string to be spliced is too long.
If on one line, the readability is too bad, if the line is changed, the error will be directly.
Here are a few tips for a few javascript concatenation strings (mainly for strings that are too long).
1. String addition (+)
var emplist = ' <li data-view-section= ' details ' > ' + '
<span>hello world</span> ' +
' </li > ';
2. Use the backslash to stitch the string
var emplist = ' <li data-view-section= ' details ' >\
<span>hello world</span>\
</li> ';
3. Using array concatenation string
Copy Code code as follows:
var emplist = [' <li data-view-section= ' details ' > ', ' <span>hello world</span> ', ' </li> '].join ( "");
Using the Join method of an array to turn an array into a string
function StringBuffer () {
this.buffer = [];
}
Adds the newly added string to the array
StringBuffer.prototype.append = function (str) {
this.buffer.push (str);
return this;
};
Convert to string
StringBuffer.prototype.toString = function () {return
This.buffer.join ("");
};
Use
var buffer = new StringBuffer ();
Buffer.append ("Hello");
Buffer.append (', World ');
Console.log (Buffer.tostring ());
Based on the array method, a class similar to StringBuffer in Java can be encapsulated to complete the concatenation of strings.
The above mentioned is the entire content of this article, I hope you can enjoy.