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

How to convert string into array in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-09 16:53:5610184browse

In JavaScript, you can use the split method to convert a string into an array. The syntax format is "element.split (string or regular expression, maximum length)". The split method is used to split a string into a string array. The strings in the returned array do not include the expression itself.

How to convert string into array in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Convert string to array

The implementation method is to cut the string into several strings according to a certain character and return it in the form of an array, sample code As follows:

var s = "abc,abcd,aaa";
ss = s.split(",");// 在每个逗号(,)处进行分解。

split() method is used to split a string into a string array.

Syntax

stringObject.split(separator,howmany)

How to convert string into array in javascript

Return value

A string array. The array is created by splitting the string stringObject into substrings at the boundaries specified by separator . The strings in the returned array do not include the separator itself.

However, if separator is a regular expression that contains subexpressions, then the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression).

Extended information:

Convert array to string

You need to concatenate the array elements into a string using a certain character. The sample code is as follows:

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

join() method is used to put all elements in the array into a string. Elements are separated by the specified delimiter.

This method returns a string. The string is generated by converting each element of the arrayObject to a string and then concatenating the strings, inserting a separator string between the two elements.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert string into array 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