This article gives you a summary of several JavaScript in the string stitching method, very simple and practical, the need for small partners can refer to.
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 (+)
?
| 1 2 3 |
var emplist = ' <li data-view-section= ' details ' > ' + ' <span>hello world</span> ' + ' </li> '; |
2. Use the backslash to stitch the string
?
| 1 2 3 |
var emplist = ' <li data-view-section= ' details ' > <span>hello world</span> </li> '; |
3. Using array concatenation string
The code is as follows:
var emplist = ['
'].join ("");
Using the Join method of an array to turn an array into a string
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 |
function StringBuffer () {this.buffer = [];}///Add 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.