How to transform string into character array?

9

Is it possible to transform string into an array of characters?

I only found the method .split(param);

I'd like to convert a string to a% of characters , a character in each index.

I would like array to become 'oi'

How to do this?

    
asked by anonymous 21.12.2016 / 04:33

2 answers

10

To separate a String by characters you can use '' as a separator.

var string = 'oi';
var array = string.split(''); // ["o", "i"]
    
21.12.2016 / 06:35
4

In addition to the split('') that Sergio has recommended, you can also access each character directly through the index, even without converting to array. For example:

var str = 'teste';
str[2] === 'e' // true
    
21.12.2016 / 18:34