Copy link to clipboard
Copied
Is it possible to split a string into array but not from the beginning?What i want is to split a word in the middle of that array.For example my string has 5 letters and i want them to start from myarray[3] and finish at myarray[8].The other elements of myarray i want to be undendifined.
It was tempting to rewrite all of your code but doing that would leave you with nothing fun to solve...
When you reassign the wordarray you do not keep any of it...
var wordarray:Array=new Array(11); // an aray of length 11
wordarray=word1.split(""); // an array of length of word1
You can determine the length of the word either using the String.length or Array.length property. Take half of 11 minus that length and assign that to the wordarray
var wordarray = new Array(int((11-word1.length)/2))
Th
...Copy link to clipboard
Copied
I am not sure what you are after regarding the split method as it does not allow for specifying how the array is processed. But you can take the resulting string and push it into an array..
var str:String = "words";
var strArray:Array = new Array(3); // create an array with 3 undefined elements
for (var i:int=0; i<str.length; i++){ // push the string characters into the array
strArray.push(str.charAt(i));
}
trace(strArray);
.
Copy link to clipboard
Copied
Maybe i didnt explain it well.I m making a game like hangman but instead of lines i have boxes.Everytime the user press a letter if this letter exists in the word then it appears in the box.On the screen i ve made 11 boxes so the word must not be higher than 11 letters.I have a string for the word named word1.I also have an integer named pickword.Everytime the user presses a button this integer takes numbers from 0 to 79.I have made 80 statements for every number comes the right word to come too. Now,what i want to do is for example:word1=apple; (apple has 5 letters) so if i use push method the word will be shown like:apple?????? (where ? means undefined).I want to be shown like this:???apple???,in the middle boxes.I want this to also happen to every word that might came.
Sorry for my bad english.
Copy link to clipboard
Copied
If you look at what I showed it does not do what you say it will do. It pushes the word into the array after the undefined portion, which in this case you have indicate should be 3 ??? (where ? means undefined).
Regardless, you should think about changing the design so that only the boxes needed are displayed. You can adjust their position if centering is desired.
Copy link to clipboard
Copied
var displayarray:Array=new Array(11);
displayarray[0]=box0txt;
displayarray[1]=box1txt;
displayarray[2]=box2txt;
displayarray[3]=box3txt;
displayarray[4]=box4txt;
displayarray[5]=box5txt;
displayarray[6]=box6txt;
displayarray[7]=box7txt;
displayarray[8]=box8txt;
displayarray[9]=box9txt;
displayarray[10]=box10txt;
var wordarray:Array=new Array(11);
var pickwords:uint=0;
pickwords=Math.floor(Math.random() * 3;
if(pickwords==0) {word1="word";}
if(pickwords==1) {word1 = "apple";}
if(pickwords==2) {word1 = "something";}
wordarray=word1.split("");
for(var p:uint=0;p<wordarray.length;p++)
{
displayarray
.text=wordarray
);
}
I want to do it like this.The word that will come to be at the middle boxes allways.Somehow to calculate how letters the word1 has and place them in the middle elements of array.Is there any way to do this?
Copy link to clipboard
Copied
It was tempting to rewrite all of your code but doing that would leave you with nothing fun to solve...
When you reassign the wordarray you do not keep any of it...
var wordarray:Array=new Array(11); // an aray of length 11
wordarray=word1.split(""); // an array of length of word1
You can determine the length of the word either using the String.length or Array.length property. Take half of 11 minus that length and assign that to the wordarray
var wordarray = new Array(int((11-word1.length)/2))
Then push the word1 contents onto the wordarray
Copy link to clipboard
Copied
thnx a lot.
Copy link to clipboard
Copied
You're welcome
Copy link to clipboard
Copied
can you explain me whats the point of int inside the parenthesis?And also what Charat means??
Copy link to clipboard
Copied
You can probably find a way to split the string here: Adobe Flash Platform * Finding substrings and patterns in strings
Find more inspiration, events, and resources on the new Adobe Community
Explore Now