Home  >  Article  >  Web Front-end  >  Detailed examples explain the difference between split and join in JavaScript

Detailed examples explain the difference between split and join in JavaScript

小云云
小云云Original
2017-12-23 11:03:362098browse

This article mainly brings you an article on the difference between split and join in javaScript (detailed explanation). What they have in common is that the split and join functions usually operate on characters or strings. The editor thinks it is quite good, so I will share it with you now and give you a reference. I hope it can help everyone.

The difference between the two:

(1) split() is used to split a string and return an array,

For example

var string="hello world?name=xiaobai";

var splitString = string.split("?");

console.log(splitString);//["hello world","name=xiaobai"]

split() has only one parameter: the split string or regular expression; when it has two parameters, the second parameter refers to the number of elements in the returned array. Number;

(2) join() is used to connect multiple characters or strings, and the return value is a string;

For example,

var arr= new Array();

arr[0]="hello";

arr[1]="xiao";

arr[2]= "bai";

arr.join("&");//"hello&xiao&bai"

join();//The default separator is comma;

Related recommendations:

JavaScript string and array conversion functions [without split and join]_javascript skills

Advanced usage skills of split and join functions in JavaScript_javascript skills

Introduction to the use of split function in JavaScript from shallow to deep

The above is the detailed content of Detailed examples explain the difference between split and join in JavaScript. 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