A brief talk on JavaScript string stitching

Source: Internet
Author: User

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 = ['

    • ', ' Hello world ', '

'].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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.