Home > Web Front-end > JS Tutorial > body text

How to connect strings in js

青灯夜游
Release: 2021-04-15 11:43:06
Original
17823 people have browsed it

JS method of connecting strings: 1. Use the string concatenation operator " " to connect strings; 2. Use the concat() method of strings to connect strings; 3. Use the join() method of arrays connection string.

How to connect strings in js

The operating environment of this article: Windows 7 system, Dell G3 computer, javascript version 1.8.5.

JS methods for connecting strings include: using the string concatenation operator " ", using the concat() method, and using the join() method.

In JavaScript, we can connect two or more strings to form a new string. This article will introduce you to the method of connecting strings in js, so that you can have some knowledge about string connections. I have a certain understanding, I hope it will be helpful to you.

String concatenation operator " "

" " " operator can be used to add strings to connect two or more string variables.

Example:

<div class="demo ">
	<p>str1="What a very"</p>
	<p>str2="nice day"</p>
	<p id="str3"></p>
</div>

<script type="text/javascript">
var str1="What a very";
var str2="nice day";
var str3= str1+" "+str2;
document.getElementById("str3").innerHTML ="str3=&#39;"+str3+"&#39;";
</script>
Copy after login

Rendering:

How to connect strings in js

In the above example, the strings str1 and str2 are changed through the " " operator There is also an empty string concatenated to form a new string str3.

Concat() method of string

The concat() method is used to concatenate two or more strings or arrays and then return A new string or array.

Syntax:

string1. concat([string2[, string3[, . . . [, stringN]]]])
Copy after login

string1: Specify a String object or string carrier, and all other specified strings can be connected to this carrier; required.

string2,. . ., stringN: The optional parameter (one or more) in the concat() method is a string appended to the end of the string1 carrier.

Note:

1. If the specified string1 carrier is different and the parameters in the concat() method are different, the new string formed will also be different.

2. If there are parameters that are not strings in the concat() method, they will be converted into strings first and then connected to the end of string1.

Example: concat() method to connect string

<div class="demo ">
	<p>str1="12q"</p>
	<p>str2="12w"</p>
	<p>str3="111"</p>
	<p id="str4"></p>
	<p id="str5"></p>
	<p id="str6"></p>
</div>

<script type="text/javascript">
var str1=&#39;12q&#39;;
var str2="12w";
var str3="111";

var str4=str1.concat(str2,str3);
var str5=str2.concat(str1,str3);
var str6=str3.concat(str1,str2);

document.getElementById("str4").innerHTML ="str4=str1+str2+str3=&#39;"+str4+"&#39;";
document.getElementById("str5").innerHTML ="str5=str2+str1+str3=&#39;"+str5+"&#39;";
document.getElementById("str6").innerHTML ="str6=str3+str1+str2=&#39;"+str6+"&#39;";
</script>
Copy after login

Rendering:

How to connect strings in js

array The join() method

join() method is used to put all the elements in the array into a string. The elements in the array will be separated by the specified delimiter.

Syntax:

arrayObject.join(separator);
Copy after login

separator parameter: used to specify the separator style to be used, which can be omitted; if omitted, a comma is used as the separator.

Example: join() method to connect strings

<div class="demo ">
	<p>arr[0]="www"</p>
	<p>arr[1]=="php"</p>
	<p>arr[2]=="cn"</p>
	<p id="str1"></p>
	<p id="str2"></p>
	<p id="str3"></p>

</div>

<script type="text/javascript">
var arr=new Array();
arr=["www","php","cn"];
var str1=arr.join(".");
var str2=arr.join("-");
var str3=arr.join(" ");
document.getElementById("str1").innerHTML ="str1=&#39;"+str1+"&#39;";
document.getElementById("str2").innerHTML ="str2=&#39;"+str2+"&#39;";
document.getElementById("str3").innerHTML ="str3=&#39;"+str3+"&#39;";
</script>
Copy after login

Rendering:

How to connect strings in js

Summary: The above is this article The entire content, I hope it will be helpful to everyone's study.

【Recommended related video tutorials: JavaScript tutorial

The above is the detailed content of How to connect strings in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!