Home >Common Problem >What is the method of converting js string to array?
The js string conversion method is: 1. split() method; 2. Array.from() method; 3. Array.prototype.split(); 4. [...string] syntax.
#In JavaScript, a string is composed of a series of characters. Sometimes, we may need to split a string into single characters or longer substrings for easier processing or further operations. This requires using the string to array method.
In JS, there are several commonly used methods to convert strings into arrays:
1. split() method
split( ) method is one of the most commonly used methods for converting strings to arrays. It splits a string into fragments and then puts the fragments into an array. We can specify the split position by specifying the delimiter.
The following is an example of using the split() method to split a string into an array:
let str = "Hello World"; let arr = str.split(" "); console.log(arr);
The output result is an array containing two elements: ['Hello', 'World'] .
As you can see, we passed a space as the separator in the split() method. This way it splits the string into two parts and puts the two parts into an array.
2. Array.from() method
The Array.from() method is one of the array static methods introduced in ES6. It creates a new array from an iterable object (including strings).
The following is an example of using the Array.from() method to convert a string into an array:
let str = "Hello World"; let arr = Array.from(str); console.log(arr);
The output result is an array containing 11 characters: ['H', 'e ', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'].
3. Array.prototype.split()
In addition to the split() method, there is also a split() method in the prototype chain of JS array, which can directly Applies to a string to convert the string to an array.
The following is an example of using the Array.prototype.split() method to split a string into an array:
let str = "Hello World"; let arr = Array.prototype.split.call(str, " "); console.log(arr);
The output result is also an array containing two elements: ['Hello' , 'World'].
It should be noted that when using the Array.prototype.split() method, it needs to be applied to the string through the call() method.
4. [...string] Syntax
In addition to using the split() method and Array.from() method, we can also use the [. ..string] syntax converts a string to an array.
The following is an example of converting a string to an array using the [...string] syntax:
let str = "Hello World"; let arr = [...str]; console.log(arr);
The output result is also an array containing 11 characters: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'].
When using the [...string] syntax, we only need to put the string in square brackets and connect it with three consecutive periods.
Summary
For the method of converting string to array, we can use split() method, Array.from() method, Array.prototype.split() method and the [...string] syntax. According to different situations and needs, just choose the appropriate method for conversion.
No matter which method is used, the converted result is an array representing string fragments or characters, which can be used for further processing and manipulation. I hope this article will help you understand and master the method of converting string to array!
The above is the detailed content of What is the method of converting js string to array?. For more information, please follow other related articles on the PHP Chinese website!