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

How to convert string to array in js

藏色散人
Release: 2020-05-11 13:34:09
Original
7302 people have browsed it

How to convert string to array in js

How to convert js arrays and strings to each other

1. Array to string

Need to use array elements Concatenate a certain character into a string. The sample code is as follows:

var a, b;
a = new Array(0,1,2,3,4);
b = a.join("-");      //"0-1-2-3-4"
Copy after login

2. Convert string to array

The implementation method is to cut the string into several strings according to a certain character, and use Return in array form, the sample code is as follows:

var s = "abc,abcd,aaa";
ss = s.split(",");// 在每个逗号(,)处进行分解  ["abc", "abcd", "aaa"]
var s1 = "helloworld";
ss1 = s1.split('');  //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
Copy after login

How to convert string to array in js

Recommended: "javascript Advanced Tutorial"

The above is the detailed content of How to convert string to array in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
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!