On _javascript techniques of JavaScript string stitching

Source: Internet
Author: User

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.

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.