if() return;
I decided to write a quick little post about the advantages of using “return”. Return works in a really cool way (for those of you who don’t know).
Traditionally you use it like this:
function returnAValue(foo):String
{
// in this case foo will be a String
return foo;
}
//outputs testing
trace(returnAValue(“testing”));
If you’ve never used return - it’s a really nice way to do calculations.
{
var finalNum:int = num1+num2;
return finalNum;
}
var newNumber = add(5, 8);
trace(newNumber); //outputs 13
Basically - it runs the function - and then gives back the caller of that function a value. It’s built so that at the return - the function stops running and returns the value you tell it to. Another standard way of using return is to use it as a way to stop a function from running. This is a really streamlined way of using a conditional.
Replacing Strings in AS3
Although this may be pretty basic - when it comes to dealing with strings, one of the most valuable methods is no doubt the split().join().
So let’s first go over split().
This is from adobe’s live docs:
split(delimiter:String, [limit:Number]) : Array
Split has two Parameters:
delimiter:String - A string; the character or string at which my_str splits.
limit:Number [optional] - The number of items to place into the array.
Ok - don’t get confused. Essentially if you have the string:
The first parameter (delimeter) is whatever character(s) you want to use for splitting the other characters up. So:
The second parameter basically tells split how many times you want to do it.
trace(myString.split(”%”, 4) // outputs: B , C , D
So a few really cool applications of this are:
1 - using it to parse out data.
2 - using it to replace or remove a string.
