Reverse a String and Reverse each Word in a String with Actionscript
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. Also where you see ‘–’ you should replace it with 2 minus signs. It’s not my fault, blame wordpress.
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 wrdArr:Array = new Array();
var rvsWrdStr:String = "";
onEnterFrame = function () {
rvsString = "";
for (i=input.text.length; i>=0; i–) { //Replace the single minus sign with 2 minus signs.
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 i 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.
rvsWrdStr = "";
for (i=0; i<wrdArr.length; i++) {
i != 0 ? rvsWrdStr += " " : 0;
for (j=wrdArr[i].length; j>=0; j–) { //Replace the — with two minus signs.
rvsWrdStr += wrdArr[i].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.
Download the source file.
Check out the other tutorials while you are here!















nice! thank you very much ^^
What is the difference betwen reverse text and reverse string? nice tut though
With reverse string the whole string is reversed. With reversed words each word in the string is reversed but they are still in the same order.
dgtegtdhgdhthth
Great, really great stuff,one more trick I would like to learn with this great code is how to reverse the line order of a pasted paragraph in the output boxes 1 and 2
That is going to be a lot harder.
Since when you type it all goes into one string which is a single line it would only work for new lines where the user has pressed ‘Enter’, since there would be ‘\n’ or ‘\r’ in that place.
thanks for your reply. Still would be possible to reverse lines of a given paragraph in a given text box so lines:
1
2
3
4
becomes
4
3
2
1
? thanks in advence.
Not that I can think of off the top of my head. Try looking in the flash help files to see if text boxes have number of line properties of something similar.
thanks.I will try.
And hey, how can you make some mc pops up when something happens?
like in the internet when you click, the box pops up.
What do you mean by “in the internet when you click, the box pops up”?
i mean, how to insert a mc from the library into the stage?
If you want to do it through actionscript you can use:
attachMovie();
Or you can just drag it out of the library onto the stage.
no, i mean like when you click a button, the mc pops up
…make sense?
Yeah. You could make a button with something like this on it:
on(release){attachMovie("mcName","newName",getNextHighestDepth());
}
The movieclip that you want to “popup” would have to have a linkage name. You just right click on it in the library to make it. Repleace “mcName” with the linkage name you give it.
“newName” will be the instance name given to the MC.
Hi, i was wondering, how do you make an input text that make something appear in a normal output a few frames later?
On the frame you would put this:
outputBox.text = inputBox.text
Hi! do you have something like this that works for php?
.hcum yrev uoy knahT .lairotut elttil ecin a si sihT
how would you make the different characters on a different line?
like
>>> ‘abc’
c
b
a
yup! that’s how i want it to be.
>>> ‘abc’
c
b
a
me too
but how do we do that?
hmm.. there’s a function in php that will revert strings. but i’m more concerned with how it is in javascript..
lets do code language
edoc egeugnal