Home  >  Article  >  Web Front-end  >  How to convert string to array in js

How to convert string to array in js

藏色散人
藏色散人Original
2020-04-26 10:25:587338browse

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"

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"]

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!

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