Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How javascript concatenates characters to form a string

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "javascript how to concatenate characters to form a string", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "javascript how to concatenate characters to form a string" this article.

Javascript concatenate characters to form a string method: 1, use the "+" plus operator, syntax "character 1 + character 2"; 2, use concat () method, syntax "character object 1.concat (character 2, character 3, character 4..., character n)"; 3, use join () method.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, there are three ways to concatenate (concatenate) characters to form strings. Let's introduce them one by one.

Method 1: use the "+" plus operator

The easiest way to connect a string is to use the plus operator.

Var S1 = "a", S2 = "b", S3 = "c", S4 = "d", S5 = "e", S6 = "f", S7 = "g"; console.log (S1 + S2 + S3 + S4 + S6 + S7); / / returns the string "abcdefg"

Output result:

Method 2: use the concat () method

Use the string concat () method to add multiple parameters to the end of a specified string. There is no limit to the type and number of parameters in this method. It converts all parameters to strings, then concatenates them sequentially to the end of the current string and finally returns the concatenated string.

Var S1 = "a", S2 = "b", S3 = "c"; var S4 = s1.concat (S2, S3, "d", "e", "f"); / / call concat () connection string console.log (S4); / / returns the string "abcdef"

Output result:

The concat () method does not modify the value of the original string, similar to the operation of the concat () method of an array.

Method 3: use the join () method

In a specific operating environment, you can also use the join () method of an array to concatenate strings, such as HTML string output.

Var s = "a", a = []; for (var I = 0; I

< 10; i ++) { a.push(s);}var str = a.join("");a = null;console.log(str); 输出结果:

The above is all the content of the article "how javascript concatenates characters to form strings". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report