0
View FavoritesView Shopping CartCheckout

Flash Tutorials

Flash Resources > Flash Tutorials > Working with Variables: Numeric operators

Working with Variables in Flash: String functions
Difficulty: Beginner

Here is an overview of basic actionscript String functions you can use to manipulate text in actionscript.


Basic Actionscript String functions

You can manipulate variables with actionscript using numeric operators such as: multiplication or subtraction. Here are some common cases of variable manipulation with numeric operators.

Isolating characters in a string with the substr() function
the substr() function has to be passed 2 parameters in order for it to return a value: start and length. The start parameter defines where to start isolating characters and the length defines how many characters to isolate. Take a look at the code below to see the substr() function in action:

studentName = "Jack Simpson";
firstName = studentName.substr(0,4); // isolate the first 4 characters starting form 0 and store them in a variable
trace(firstName); // Displays: Jack

Finding characters in a string with the indexOf() function
The indexOf() function is a very useful one. It can find the first occurrence a character in a string and return its position. If no occurrence is found, the function will return -1. Let's say you want to validate that an email is valid in it's form, you would have to verify three things:

1. There is an "@" in the email
2. There is a "." in the email
3. There's no empty spaces in the email

Let's look at how we would do this with actionscript:

email = "john@website.com";
isValid = true;
if (email.indexOf("@") == -1) {
    isValid = false;
}
if (email.indexOf(".") == -1) {
    isValid = false;
}
if (email.indexOf(" ") != -1) {
    isValid = false;
}
trace(isValid); // displays: true because in this case the email is a valid one.

Finding the character at a specific position in a string with the charAt() function
Using the charAt() function is pretty simple, let's say you want to put the first character of a string in a variable, you could write the actonscript code below:

firstName = "John";
firstChar = firstName.charAt(0);
trace(firstChar); // displays: "J" the first character of the firstName variable.

Note: actionscript is a zero-based language, therefore the first element in a list is element 0 not element 1. That's why in this case firstName.charAt(0); returns "J".

Counting the number of letters in a string
You can find the number of characters in a string by accessing the length property. Below is a simple example of how you can count letters in a string with actionscript.

string1 = "Hello";
nbLetters = string1.length;
trace(nbLetters); // displays: 5 the number of character in the string1 variable.

Next Flash Tutorial > Working with variables part 3 (coming soon)


Beginner Flash Tutorials
Variables Basics Tutorial    
Working with Variables Part 1    
Working with Variables Part 2    
How to make a simple flash button    
How to Make a Flash Preloader    
   

 

 
paypal approved 

 
Design by Scalpel