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
  • 48,619
  • 13
  • 99
  • 109
user1752557
  • 45
  • 1
  • 1
  • 6

5 Answers5

3

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

KooiInc
  • 112,400
  • 31
  • 139
  • 174
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
  • 22,932
  • 5
  • 63
  • 83
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,059
  • 5
  • 34
  • 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,670
  • 10
  • 42
  • 73
0

Try using this code:-

var array = str.split('');

Look at the following page to know more about splits.

Javascript split

noob
  • 18,447
  • 20
  • 113
  • 179
Talha
  • 18,147
  • 8
  • 47
  • 64