Get the App
SLTechnology News&Howtos  ›  Development  › 

How javascript concatenates characters to form a string

Shulou Source: shulou.com Published: 2022-06-03 18:44:12 10月27日 Update

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!

Tags: Character string method output content plus sign parameter article result operator operation tail array environment grammar learning help similarity simplicity number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Apple macOS Redmi vpn