JavaScript String concatenation-javascript tips-js tutorial

Source: Internet
Author: User
Tags javascript string concatenation
This article summarizes several methods of String concatenation in javascript, which are very simple and practical. For more information, see. 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 = ' 
  • '+ 'Hello world'+ '
  • ';

    2. concatenate a string using a backslash

    var empList = ' 
  • \ Hello world\
  • ';

    3. concatenate strings using Arrays

    The Code is as follows:


    Var empList = ['

  • ', 'Hello world ','
  • ']. 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.

    Related Article

    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.