Home  >  Article  >  Web Front-end  >  How to concatenate elements into a string using join() method

How to concatenate elements into a string using join() method

不言
不言Original
2018-12-21 17:41:587258browse

The join() method joins elements into strings: first declare an array through var; then connect the string specified in the array through the "array.join('');" method.

How to concatenate elements into a string using join() method

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

The join method in How to concatenate elements into a string using join() method can put all the elements in the array divided by the specified delimiter into a string. Let's take a look at the specific method of joining strings in How to concatenate elements into a string using join() method using the join() method.

How to concatenate elements into a string using join() method

When we connect strings, we first need to know how to use the plus sign to connect

console.log("php"+"中文网");

Run results: php Chinese network

Similarly, in the join method, when concatenating the strings specified in Array (array), it is written as follows.

var array = ["php", "中文网"];
var result = array.join('');
console.log( result );

Display results: php Chinese website

Usage of join method

The join method has the following usage methods

join()
join(str)

Usage of join()

When using join(), no parameters are specified.

Parameters are the values ​​to be passed to the method (function) when using the method (function).

If executed like this without specifying parameters, it will be treated as if a comma (",") is specified as a parameter for php, Chinese website, etc., as shown below.

var array = ["php", "中文网"];
var result = array.join();
console.log( result );

Display results: php, Chinese website

Usage of join(str)

In the case of join(str), Specify a string in str (str is the abbreviation of string, which means string.)

By specifying join(str) as join(":"), the program is displayed as php: Chinese website.

var array = ["php", "中文网"];
var result = array.join(':');
console.log( result );

Display results: php: Chinese website

How to concatenate elements into a string using join() method

Let’s look at a specific example

Prepare five empty Arrays (arrays) with no values.

Then, connect the empty Array (array) with the PHP Chinese website.

Empty Array (array) is an empty element (does not contain anything), the result will be displayed as: php Chinese network, php Chinese network, php Chinese network, php Chinese network.

But, in fact, (empty element) php中文网 (empty element) php中文网 (empty element) php中文网 (empty element) php中文网 (empty element) etc., (empty element) php Chinese network strings will be connected in this way.

var str = 'php中文网,';
var result = Array(5).join( str );
console.log( result );

The displayed results are as follows

How to concatenate elements into a string using join() method

By using join, we can perform various connection processing.

In addition to join, there are other types of connection processing, such as the use of =, the use of concat, etc.

The above is the detailed content of How to concatenate elements into a string using join() method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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