0

I have a string for e.g:

        var str = 'abcdef'; 

i want to get it in an array format e.g:

        var a;
        a[0]=a;
        a[1]=b;  and so on.. 

i am aware of split method but in this case of string without any space how can i split as individual character??

Ian
  • 50,146
  • 13
  • 101
  • 111
user1752557
  • 45
  • 1
  • 1
  • 7

5 Answers5

3

Use: str.split(''). That will create an array with characters of str as elements.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
2

You can access it like this ,

var str='abcdef'; 
alert(str[0]);

or

var str='abcdef'; 
alert(str.charAt(0));

refer following link to chose which is the best way. http://blog.vjeux.com/2009/javascript/dangerous-bracket-notation-for-strings.html

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
0
var s = "abcdef";
var a;
for (var i = 0; i < s.length; i++) {
    a.push(s.charAt(i));
}

or

 var s = "abcdef";
 var a;
 var a= s.split('');
Cris
  • 12,799
  • 5
  • 35
  • 50
0
var str="abcdef";
var a=new Array;
for (var x=0; x<str.length; x++) {
    a[x] = str.charAt(x);
}
console.log(a);
Antony
  • 14,900
  • 10
  • 46
  • 74
0

Try using this code:-

var array = str.split('');

Look at the following page to know more about splits.

Javascript split

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
Talha
  • 18,898
  • 8
  • 49
  • 66