Reverse a String and Reverse each Word in a String with Actionscript - Page 1
Posted by : awesty on Apr 21, 2008
DOWNLOAD THE SOURCE FILE FOR THIS TUTORIAL
In this tutorial you will learn how to reverse a string ('This is a string' to 'gnirts a si sihT') and how to reverse each word in a string but still keep the words in the same order ('This is a string' to 'sihT si a gnirts'). You can see an example below. I am using Flash CS3/9, but Flash MX 2004 and Flash 8 will work as well. If you are using Flash CS3 make sure you select as AS 2.0 Flash file.
NOTE: If you copy and paste the code you may need to rewrite the quotation marks ("") because flash doesn't like this font.
Open up flash and make 3 text boxes like above. Make one and input text box with an instance name of input and the other two dynamic text boxes with instance names ouput1 and output2.
Select the frame they are on and open the actions panel. Enter this code:
var rvsString:String = "";
var wrdArr:Array = new Array();
var rvsWrdStr:String = "";
onEnterFrame = function () {
rvsString = "";
for (i=input.text.length; i>=0; iâ??-) {
rvsString += input.text.charAt(i);
}
output1.text = rvsString;
}
First of all we are declaring 3 variables. Two of them are strings (rvsString and rvsWrdStr) and the other is an array. rvsString will hold the reversed string, rvsWrdStr will hold the reversed word string and wrdArr will hold each word.
We are using an onEnterFrame function so that everytime the frame is entered the script inbetween the curly braces is run. rvsString is made equal to blank because otherwise you would end up with something like TThThiThisThis etc. We then make a for loop where is equal to the length of the input text. Each time the loop is run i decreases by one. The script between the curly braces of the for loop is run until i is less than 0.
Each time the for loop is run a letter gets added onto the rvsString string. It adds the character from the input text at the index of i. In the string 'Hello', H is at the index 0, e is 1, o is 4. Since i starts as the number equal to the index of the last letter, the letters get added in rvsString backwards.
After the for loop has finished 'output1? equals rvsString, so it is equal to 'input' written backwards.
If you test that when you enter text into the 'input' text box it should come out reversed in the 'output1? text box.
Now enter this code just before the last curly brace.
wrdArr = input.text.split(" ");
rvsWrdStr = "";
for (i=0; i<wrdArr.length; i++) {
i != 0 ? rvsWrdStr += " " : 0;
for (j=wrdArr*.length; j>=0; jâ??-) {
rvsWrdStr += wrdArr*.charAt(j);
}
}
output2.text = rvsWrdStr;
The first line reads the input text and wherever there is a space it will split the string and put it into the wrdArr. So if the input text was "Hello world!", wrdArr[0] would be Hello and wrdArr[1] would be world!.
Next we make another for loop which loops through each word in the wrdArr. The next line checks if i is equal to 0. If it isnâ??t it adds a space onto the end of the rvsWrdStr variable. Otherwise there would be a space at the start of the string.
We make another for loop. This one goes through each letter in the word that the other for loop is up to backwards. The next line adds the letter into the string. It does the words in order, but the letters of the words backwards so we end up with backward words in the right order. The last line puts it into the 'output2' textbox.




no comment
Add comment