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