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

Detailed interpretation of the code, string concatenation in Javascript

亚连
Release: 2018-05-17 17:43:45
Original
1973 people have browsed it

The following is the String connection in Javascript that I compiled for you. Interested students can take a look.

<pre name="code" class="javascript"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
<script type="text/javascript">  
/*1.concat函数,将两个或者多个字符串连接成一个字符串  
    首先看一下concat的语法  
    variable.concat("string1","string2","string3","string4");  
*/  
    var str1 = "aa"  
    var str2 = "bb"  
    var str3 = "cc"  
    var str = str1.concat(str2,str3);  
    alert(str);  
//或  
    str = "";  
    str = str.concat(str1,str2,str3);  
    alert(str);  
//或  
    str = "";  
    str = str.concat("aa","bb","cc");  
    alert(str);  
/*2. 通过加号(+)直接对字符串进行连接  
*/  
    str = str1+str2+str3;  
    alert(str);  
//或 声明一个变量来进行连接  
    str = "";  
    str += "hello";  
    str += "world";  
    document.write(str);  
/*3. 将字符串存入数组,然后调用join方法.  
将数组中的每个元素连接起来返回一个字符串.参数就是连接符.(默认连接符是",")  
使用该方法可以模拟一个StringBuilder  
*/  
    var sArr = ["long"," time"," no"," see"];  
    alert(sArr.join(""));  
</script>  
</head>  
<body>  
</body>  
</html>
Copy after login

The above is the string connection in Javascript that I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Focus on answering several submission methods of js

The most practical answers to questions in JS development

Detailed explanation of the Windows Object Course in JS

The above is the detailed content of Detailed interpretation of the code, string concatenation in Javascript. 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!