Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

split method but not from the beginning of array

Explorer ,
May 21, 2015 May 21, 2015

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.

TOPICS
ActionScript
863
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 22, 2015 May 22, 2015

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

...
Translate
LEGEND ,
May 22, 2015 May 22, 2015

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);

.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 22, 2015 May 22, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 22, 2015 May 22, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 22, 2015 May 22, 2015

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

);

}

box.jpg

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 22, 2015 May 22, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 22, 2015 May 22, 2015

thnx a lot.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 22, 2015 May 22, 2015

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 23, 2015 May 23, 2015
LATEST

can you explain me whats the point of int inside the parenthesis?And also what Charat means??

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 22, 2015 May 22, 2015

You can probably find a way to split the string here: Adobe Flash Platform * Finding substrings and patterns in strings

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines