Talking about JavaScript String concatenation and javascript concatenation
String concatenation is often encountered in JavaScript, but it is troublesome if the string to be concatenated is too long.
The readability of a line is too poor. If the line breaks, an error is reported.
Now we will introduce several tips on how to splice strings with JavaScript (mainly for the case of long strings ).
1. String addition (+)
var empList = ' <li data-view-section="details">'+ '<span>Hello world</span>'+ '</li>';
2. concatenate a string using a backslash
var empList = ' <li data-view-section="details">\ <span>Hello world</span>\ </li>';
3. concatenate strings using Arrays
Copy codeThe Code is as follows:
Var empList = ['<li data-view-section = "details">', '<span> Hello world </span>', '</li>']. join ("");
Converts an array to a string using the join method of the array.
Function StringBuffer () {this. buffer = [];} // Add the newly added string to the StringBuffer array. prototype. append = function (str) {this. buffer. push (str); return this ;}; // convert it to a string StringBuffer. prototype. toString = function () {return this. buffer. join ("") ;}; // usage 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 String concatenation.
The above is all the content of this article. I hope you will like it.