Analyzing and comparing characters in strings

Every character in a string has an index position in the string (an integer). The index position of the first character is 0. For example, in the string yellow, the character y is in position 0 and the character w is in position 5.

Every string has a length property, which is equal to the number of characters in the string:

var companyStr:String = "adobe";
trace(companyStr.length); // 10

An empty string and a null string both have a length of zero:

var firstStr:String = new String();
trace(firstStr.length); // 0

var secondStr:String = "";
trace(secondStr.length); // 0

If a string doesn't contain a value, its length is set to undefined:

var thirdStr:String;
trace(thirdStr.length); // undefined

WARNING

If your string contains a null byte character (\0), the string value will be truncated.

You can also use character codes to define a string. For more information on character codes and character encoding, see About strings and the String class.

The following example creates a variable named myStr and sets the string's value based on ASCII values passed to the String.fromCharCode() method:

var myStr:String = String.fromCharCode(104,101,108,108,111,32,119,111,114,108,100,33);
trace(myStr); // hello world!

Each number listed in the fromCharCode() method in the previous code represents a single character. For example, the ASCII value 104 represents a lowercase h, and the ASCII value 32 represents the space character.

You can also use the String.fromCharCode() method to convert Unicode values, although the unicode value must be converted from hexadecimal to decimal values, as shown in the following ActionScript:

// Unicode 0068 == "h"
var letter:Number = Number(new Number(0x0068).toString(10));
trace(String.fromCharCode(letter)); // h

You can examine the characters in various positions in a string, as in the following example.

To loop over a string:

  1. Create a new Flash document.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var myStr:String = "hello world!";
    for (var i:Number = 0; i < myStr.length; i++) {
        trace(myStr.charAt(i));
    }
    
  3. Select Control > Test Movie to preview your Flash document. You should see each character traced to the Output panel on a separate line.
  4. Modify the existing ActionScript code so that it traces the ASCII value for each character:
    var myStr:String = "hello world!";
    for (var i:Number = 0; i < myStr.length; i++) {
        trace(myStr.charAt(i) + " - ASCII=" + myStr.charCodeAt(i));
    }
    
  5. Save the current Flash document and select Control > Test Movie to preview the SWF file.

    When you run this code, the following is displayed in the Output panel:

    h - ASCII=104
    e - ASCII=101
    l - ASCII=108
    l - ASCII=108
    o - ASCII=111
      - ASCII=32
    w - ASCII=119
    o - ASCII=111
    r - ASCII=114
    l - ASCII=108
    d - ASCII=100
    ! - ASCII=33
    

TIP

You can also split a string into an array of characters by using the String.split() method and entering an empty string ("") as a delimiter; for example, var charArray:Array = myStr.split("");

You can use operators to compare strings. For information on using operators with strings, see About using operators with strings.

You can use these operators with conditional statements, such as if and while. The following example uses operators and strings to make a comparison.

To compare two strings:

  1. Create a new Flash document and save it as comparestr.fla.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var str1:String = "Apple";
    var str2:String = "apple";
    if (str1 < str2) {
        trace("Uppercase letters sort first.");
    }
    
  3. Save the Flash document and select Control > Test Movie to test the SWF file.

You can use the equality (==) and inequality (!=) operators to compare strings with other types of objects, as shown in the following example.

To compare strings to other data types:

  1. Create a new Flash document and save it as comparenum.fla.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var myStr:String = "4";
    var total:Number = 4;
    if (myStr == total) {
        trace("Types are converted.");
    }
    
  3. Save the Flash document and select Control > Test Movie to test the SWF file.

    When comparing two different data types (such as strings and numbers), Flash tries to convert the data types so that a comparison can be made.

Use the strict equality (===) and strict inequality (!==) operators to verify that both comparison objects are of the same type. The following example uses strict comparison operators to ensure that Flash doesn't try to convert data types while trying to compare values.

To force strict data type comparisons:

  1. Create a new Flash document and save it as comparestrict.fla.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var str1:String = "4";
    var str2:String = "5";
    var total:Number = 4;
    if (str1 !== total) {
        trace("Types are not converted.");
    }
    if (str1 !== str2) {
        trace("Same type, but the strings don't match.");
    }
    
  3. Save the Flash document and select Control > Test Movie.

For more information on using operators with strings, see About using operators with strings.

For a sample source file, strings.fla, that shows you how to build a simple word processor that compares and retrieves string and substring selections, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/Strings folder to access the sample.