Best viewed in Firefox

Awesty Productions

Reverse a String and Reverse each Word in a String with Actionscript

September 17th, 2007 by awesty

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. :P


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.

Input and Dynamic text boxes

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–) { //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.

    wrdArr = input.text.split(" ");
    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!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

25 Comments »

Comment by zimius Subscribed to comments via email
2007-09-23 00:48:23

nice! thank you very much ^^

 
Comment by Waratica Subscribed to comments via email
2007-09-30 15:41:05

What is the difference betwen reverse text and reverse string? nice tut though

Comment by awesty
2007-09-30 15:56:47

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.

 
 
Comment by avi Subscribed to comments via email
2007-10-15 21:37:48

dgtegtdhgdhthth

 
Comment by flash_newbee Subscribed to comments via email
2007-11-03 08:31:04

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

Comment by awesty
2007-11-03 15:45:50

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.

 
 
Comment by flash_newbee Subscribed to comments via email
2007-11-03 18:39:46

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.

Comment by awesty
2007-11-03 23:06:00

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.

 
 
Comment by flash_newbee Subscribed to comments via email
2007-11-04 08:03:57

thanks.I will try.

 
Comment by Waratica Subscribed to comments via email
2007-11-05 22:20:33

And hey, how can you make some mc pops up when something happens?
like in the internet when you click, the box pops up.

Comment by awesty
2007-11-06 09:37:37

What do you mean by “in the internet when you click, the box pops up”?

Comment by Warrantica Subscribed to comments via email
2007-11-06 21:12:23

i mean, how to insert a mc from the library into the stage?

Comment by awesty
2007-11-07 16:33:03

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.

Comment by Waratica Subscribed to comments via email
2007-11-07 22:04:22

no, i mean like when you click a button, the mc pops up

…make sense?

(Comments wont nest below this level)
Comment by awesty
2007-11-08 16:39:19

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.

 
 
 
 
 
 
Comment by FlashLord Subscribed to comments via email
2008-01-18 05:22:17

Hi, i was wondering, how do you make an input text that make something appear in a normal output a few frames later?

Comment by awesty
2008-01-18 15:09:47

On the frame you would put this:

outputBox.text = inputBox.text

 
 
Comment by Jade Subscribed to comments via email
2008-03-06 07:41:10

Hi! do you have something like this that works for php?

 
Comment by Andy123 Subscribed to comments via email
2008-03-23 04:23:44

.hcum yrev uoy knahT .lairotut elttil ecin a si sihT

 
Comment by Megan Subscribed to comments via email
2008-04-26 08:33:11

how would you make the different characters on a different line?
like
>>> ‘abc’
c
b
a

 
Comment by Jade Subscribed to comments via email
2008-04-26 08:35:13

yup! that’s how i want it to be.

>>> ‘abc’
c
b
a

 
Comment by Megan Subscribed to comments via email
2008-04-26 08:44:55

me too
but how do we do that?

 
Comment by Jade Subscribed to comments via email
2008-04-26 13:58:03

hmm.. there’s a function in php that will revert strings. but i’m more concerned with how it is in javascript..

 
Comment by rudydbooty
2008-07-19 07:33:48

lets do code language
edoc egeugnal

 
Comment by ilike2flash
2009-01-20 23:15:38

Great, this is a nice effect.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.