Friday, May 14, 2010

string to array

View Full Version : string to array

nishithdoshi
04-23-2004, 04:11 AM
hi,

i am having a string which i want to show in array. How can i do that ?

//string
str1 = "Hello";

//Result expected :-

result_str1 ="H","e","l","o"'

MyArray = new Array(result_str1);

trace(MyArray[0]) // H will be shown
tripleaxis
04-23-2004, 06:45 AM
use the 'split' method of the string object.

in other words, when you have a string in a variable, you can say: myArray = myVariable.split();

and it'll break apart the entire string into letters and place each letter into a separate slot in the array. You can also tell it to split the string on different letters or words, eg:
var myVariable = "Hello";
var myArray = myVariable.split( "e" );
//myArray will contain:
"H", "llo";

hope this helps.

No comments:

Post a Comment