How to concatenate a large number of strings in Javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to splice a large number of strings in Javascript. This article implements the Heredoc syntax in JS language, for more information about how to define strings in heredoc mode in php and python, see:

Php:

The Code is as follows:


$ SQL = < Select *
From pages
Where pagename = '$ pn'
EOD;


Python:

The Code is as follows:


Print """
This is an example of a string in the heredoc syntax.
This text can span multiple lines
"""


Js concatenates a large number of strings without heredoc-style operators, which is complicated:

Splicing Method 1:

The Code is as follows:


Var str = "\
Here is line one \
And line two \
Finally, line three! \
";

Alert (str );


Splicing Method 2:

The Code is as follows:


Var _ template =
''+
'# SalarySN #'+
'# Name #'+
'# TDR_NAME #'+
'# TSD_NAME #'+
'# WORK_STATUS #'+
'# Isleader_display #'+
''
+ 'Set role'
+'';


JS Strings need to break the original string style, and each line is processed, which is a bit unbearable.

Provide a solution:

The Code is as follows:


Function aHereDoc (){/*
Hello, World!
I am a JavaScript here document.
Use the 'heredoc' function to extract me.
*/}

Function hereDoc (func ){
Return func. toString (). split (/\ n/). slice (1,-1). join ('\ n ');
}
Console. log (hereDoc (aHereDoc ));


Use func. toString () to obtain the string to be processed in batches. Use split (/\ n /). slice (1,-1) removes the first and last line of function-defined code and reassembles 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.