Forum Topic: AS: Manipulating Strings

(1,454 views • 9 replies)

This topic is 1 page long.

<< < > >>
Shouting

SpamBurger

Reply To Post Reply & Quote

Posted at: 2/6/06 06:24 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

AS: Main

Ok, in this tutorial, you will be learning different ways to manipulate a string. I will be covering different methods, functions, etc. So, put on your learning caps and lets learn!
-----------------
Making a String
Ok, this part is very basic. But, if you dont know what a string is or how to make one, read below vv

What is a string?
A string is, in general, a group of characters all tagged together. Strings are useful becuase without them, there would be no way to make dynamic text.

How do I make a string?
To make a string, you must put double quotes ("") or single quotes ('') around your string. So, below I will show you what is correct, and what isnt.

Not Correct:

SpamBurger Rocks

Correct:

"SpamBurger Rocks" or 'SpamBurger Rocks'

You may ask why there has to be quotes, well, there is a good reason. If you didnt have quotes, flash would treat your so-called "string" as a variable. If you didnt have a variable with the name of your so-called "string", then flash would return "undefined" if you were to try and trace it. So, to get around this, we put quotes around our string to make flash force it as a string, and not a variable.
-----------------
Now, I shall go over different functions and methods.
-----------------
Converting an Expression to a String
To convert an expression to a string, you would use one simple function:

String(expression);

Doing that would turn whatever expression is in the parentheses (sp?) to an expression. Im not totally sure why it's useful, but Im assuming its becuase you can easily manipulate it later.
-----------------
Finding a Character's Posistion in a String

To find a character's position in a string, again, you only need one simple function. It is below:

string.indexOf('char whos spot u want find');

The above function will return an integer. It will return the character's spot in the string. This function is useful becuase it is a good way of checking for characters in a string, or making sure characters arent in a string. An example is below:

"spamburger".indexOf('s'); would return 0 becuase 's' is in the 0 spot of the string. An example of making sure characters are in a string is below:


gosh="dude^balls?";
damn=gosh.indexOf('^');
if(damn!=-1) {
//execute code
}

That above code will execute only if the char '^' is in the string of 'dude^balls?'. We check to see if the posistion of '^' is not -1 becuase if you are ever returned -1, that means the char is not within the string. So, we make sure that the return value is not -1 before executing the code. Sorry is that was a little confusing.
-----------------
lastIndexOf();
This function is sort of like the indexOf() function, but instead of searching the string from left to right, it searches it from right to left. Becuase this is simillar, I wont explain it as much.

What this function does is it returns the last occurence of a character, instead of the first occurence. Examples below:


hi="helloo";
bob=hi.indexOf('o');
trace(bob);

Using the indexOf() function, you will be returned 4.


hi="helloo";
bob=hi.lastIndexOf('o');
trace(bob);

Using the lastIndexOf() function, you will be returned 5. Sorry if I didnt explain this very well, but, it was hard since they were so simillar.
-----------------
Finding the Amount of Characters in a String

Using one simple property, you can find the amount of characters in a string. This is helpful becuase you may be making a form, and you dont want people entering large words. The code is below:

string.length;

Doing that will return you an integer with the value of the amount of characters in a string. An example code is below:


spam="spammy!";
spam_length=spam.length;
trace(spam_length);

If you enter that code, you will be returned the number 7. This is becuase there are 7 characters in the string "spammy!".
-----------------
Converting a String to an Array

The code I am about to give you will be very helpful. Using the split() function, you can turn a string into an array depending on where the separator is. The code is below:

string.split('separator');

The separator can be anything. It can be single characters, or words. An example code is below:


spamburger="hi-nice-birds";
bob=spamburger.split('-');
trace(bob);

The above code will return "hi,nice,birds". If you want to return just one value of bob, we treat bob as an array. So, bob[1] would return "nice".
-----------------
Please read on for the next part of this tutorial vv

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 2/6/06 06:26 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Joining Strings Together

I will now explain a fairly useless function, but, I guess it's worth mentioning. It is the concat() function. It is used to join strings into one big string. The code is below:

string.concat('value1','value2',....);

That will take a string, and add all the values within the parentheses and add them to the end of the string. An example code is below:


spam_mail="spamburger ";
spam_posts=spam_mail.concat("rocks ","out ","loud!");
trace(spam_posts);

If you enter the code, you will be returned "spamburger rocks out loud!". Instead of using the concat() function, you should really just use + to add strings together. Instead of having a 3 line code, we could have a one line code by doing the following:


trace("spamburger "+"rocks "+"out "+"loud!");

I persoanlly prefer this method, but if you feel safer with the concat() function, go ahead and use it.
-----------------
Changing Lower Case Letters to Upper Case

A pretty useless function, but I will go over it. The toUpperCase() function will change all the lower case characters in a string, to upper case. The code is below:

string.toUpperCase();

That will change all lower case letters to upper case. An example code is below:


authorblues="a fugly retard";
spam_ownz=authorblues.toUpperCase();
trace(spam_ownz);

That above code will return "A FUGLY RETARD" in big capital letters :]
-----------------
Changing Upper Case Letters to Lower Case

A pretty useless function, but I will go over it. The toLowerCase() function will change all the upper case characters in a string, to lower case. The code is below:

string.toLowerCase();

That will change all upper case letters to lower case. An example code is below:


authorblues="A FUGLY RETARD";
spam_ownz=authorblues.toLowerCase();
trace(spam_ownz);

That above code will return "a fugly retard" in small lower case letters :]
-----------------
Well, that concludes my tutorial. I now have a big headache. Please post any comments/questions you have here. Thank you.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

authorblues

Reply To Post Reply & Quote

Posted at: 2/6/06 06:29 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 2/6/06 06:26 PM, SpamBurger wrote: authorblues="A FUGLY RETARD";
spam_ownz=authorblues.toLowerCase();
trace(spam_ownz);

your mom is a fugly retard.

just for the sake of debate, id like to note that, although it works, i have always been taught that var.length is appropriate for arrays, but for strings, you should use length(var). still, pretty good job. pretty generic topic, but why the hell not...

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

SpamBurger

Reply To Post Reply & Quote

Posted at: 2/6/06 07:04 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 2/6/06 06:29 PM, authorblues wrote: your mom is a fugly retard.

I know T.T


just for the sake of debate, id like to note that, although it works, i have always been taught that var.length is appropriate for arrays, but for strings, you should use length(var).

Cool, thanks for that info.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Denvish

Reply To Post Reply & Quote

Posted at: 2/6/06 07:20 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

Who is this bob character?

- - Flash - Music - Images - -

BBS Signature

None

SpamBurger

Reply To Post Reply & Quote

Posted at: 2/6/06 07:38 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 2/6/06 07:20 PM, Denvish wrote: Who is this bob character?

Ha ha. I always use bob as a variable name in my example codes. But, now I just remembered that bob is your name, so, umm............... who wants pie?

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


Happy

JeremysFilms

Reply To Post Reply & Quote

Posted at: 4/7/06 05:04 PM

JeremysFilms NEUTRAL LEVEL 17

Sign-Up: 02/18/05

Posts: 1,522

Nice work Spammerlla :D! You're no longer a b00n ;).


None

sandypaw

Reply To Post Reply & Quote

Posted at: 7/20/07 09:08 PM

sandypaw DARK LEVEL 10

Sign-Up: 06/28/07

Posts: 1,268

At 2/6/06 06:24 PM, SpamBurger wrote:
What is a string?
A string is, in general, a group of characters all tagged together. Strings are useful becuase without :them, there would be no way to make dynamic text.

Wrong! a string is something that you dangle in front of your cats and they swat at it!

=3


None

DarkMana

Reply To Post Reply & Quote

Posted at: 7/20/07 09:10 PM

DarkMana DARK LEVEL 23

Sign-Up: 04/04/05

Posts: 2,728

At 7/20/07 09:08 PM, sandypaw wrote: Wrong! a string is something that you dangle in front of your cats and they swat at it!

You bumped a year-old topic for that?

Asus P5Q PRO, Intel E8400 @ 3.60 GHz, 4GB DDR2-1000, ATI HD4850

BBS Signature

None

FriedBabySeal

Reply To Post Reply & Quote

Posted at: 7/20/07 09:19 PM

FriedBabySeal LIGHT LEVEL 06

Sign-Up: 07/14/07

Posts: 101

Heck yes he did!!!


All times are Eastern Standard Time (GMT -5) | Current Time: 04:03 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!