Home  >  Article  >  Web Front-end  >  Convert string to character array javascript

Convert string to character array javascript

PHPz
PHPzOriginal
2023-05-17 22:22:09832browse

During the development process, we often need to convert a string into a character array. In JavaScript, this process is not complicated, and we can complete it with some simple operations. This article will introduce how to convert a string into a character array in JavaScript.

Method 1: Use the split() method

The split() method in JavaScript can split a string into an array. Passing an empty string as argument to the split() method splits the string into individual characters.

The following is a sample code that uses the split() method to convert a string into a character array:

let str = 'JavaScript'
let arr = str.split('')
console.log(arr)

Run the above code, the console will print out the following results:

[ "J", "a", "v", "a", "S", "c", "r", "i", "p", "t" ]

In this example, we use the split() method to split the string into an array of characters. Since we passed an empty string as argument, each character in the string is split into a new array element.

Method 2: Use the Array.from() method

The Array.from() method can convert an array-like object or an iterable object into a real array. We can use this to convert a string into a character array.

The following is a sample code that uses the Array.from() method to convert a string into a character array:

let str = 'JavaScript'
let arr = Array.from(str)
console.log(arr)

Compared with the above sample code, this sample code just split() method Changed to Array.from() method. Run the above code, the console will print out the following results:

[ "J", "a", "v", "a", "S", "c", "r", "i", "p", "t" ]

Method 3: Use for loop conversion

We can also use a simple for loop to convert the string into a character array.

The following is a sample code that uses a for loop to convert a string into a character array:

let str = 'JavaScript'
let arr = []
for (let i = 0; i < str.length; i++) {
  arr.push(str.charAt(i))
}
console.log(arr)

In this example, we first define an empty array arr. We then use a loop to iterate through each character in the string and add each character to the array using the push() method. Run the above code, the console will print out the following results:

[ "J", "a", "v", "a", "S", "c", "r", "i", "p", "t" ]

Summary

The above are three methods to convert a string into a character array. In actual development, we can choose to use one of them according to the actual situation. If you have a better method, please share it in the comment section.

The above is the detailed content of Convert string to character array 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