Converting and concatenating strings

You can use the toString() method to convert many objects to strings. Most built-in objects have a toString() method for this purpose:

var n:Number = 0.470;
trace(typeof(n.toString())); // string

When you use the addition (+) operator with a combination of string and nonstring instances, you don't need to use the toString() method. For details on concatenation, see the second procedure in this section.

The toLowerCase() method and the toUpperCase() method convert alphabetical characters in the string to lowercase and uppercase, respectively. The following example demonstrates how to convert a string from lowercase to uppercase characters.

To convert a string from lowercase to uppercase:

  1. Create a new Flash document and save it as convert.fla.
  2. Type the following code on Frame 1 of the Timeline:
    var myStr:String = "Dr. Bob Roberts, #9.";
    trace(myStr.toLowerCase()); // dr. bob roberts, #9.
    trace(myStr.toUpperCase()); // DR. BOB ROBERTS, #9.
    trace(myStr); // Dr. Bob Roberts, #9.
    
  3. Save the Flash document and select Control > Test Movie.

    NOTE

    After these methods are executed, the source string remains unchanged. To transform the source string, use the following:

    myStr = myStr.toUpperCase();
    

When you concatenate strings, you take two strings and join them sequentially into one string. For example, you can use the addition (+) operator to concatenate two strings. The next example shows you how to concatenate two strings.

To concatenate two strings:

  1. Create a new Flash document and save it as concat.fla.
  2. Add the following code to Frame 1 of the Timeline:
    var str1:String = "green";
    var str2:String = str1 + "ish";
    trace(str2); // greenish
    //
    var str3:String = "yellow";
    str3 += "ish";
    trace(str3); // yellowish
    

    The preceding code shows two methods of concatenating strings. The first method uses the addition (+) operator to join the str1 string with the string "ish". The second method uses the addition and assignment (+=) operator to concatenate the string "ish" with the current value of str3.

  3. Save the Flash document and select Control > Test Movie.

You can also use the concat() method of the String class to concatenate strings. This method is demonstrated in the following example.

To concatenate two strings with the concat() method:

  1. Create a new Flash document and save it as concat2.fla.
  2. Add the following code to Frame 1 of the Timeline:
    var str1:String = "Bonjour";
    var str2:String = "from";
    var str3:String = "Paris";
    var str4:String = str1.concat(" ", str2, " ", str3);
    trace(str4); // Bonjour from Paris
    
  3. Select Control > Test Movie to test the Flash document.

If you use the addition (+) operator (or the addition and assignment [+=] operator) with a string and a nonstring object, ActionScript automatically converts nonstring objects to strings in order to evaluate the expression. This conversion is demonstrated in the following code example:

var version:String = "Flash Player ";
var rel:Number = 9;
version = version + rel;
trace(version); // Flash Player 9

However, you can use parentheses to force the addition (+) operator to evaluate arithmetically, as demonstrated in the following ActionScript code:

trace("Total: $" + 4.55 + 1.46); // Total: $4.551.46
trace("Total: $" + (4.55 + 1.46)); // Total: $6.01

You can use the split() method to create an array of substrings of a string, which is divided based on a delimiter character. For example, you could segment a comma- or tab-delimited string into multiple strings.

For example, the following code shows how you can split an array into substrings by using the ampersand (&) character as a delimiter.

To create an array of substrings segmented by delimiter:

  1. Create a new Flash document and save it as strsplit.fla.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var queryStr:String = "first=joe&last=cheng&title=manager&startDate=3/6/65";
    var params:Array = queryStr.split("&", 2);
    trace(params); // first=joe,last=cheng
            /* params is set to an array with two elements:
                params[0] == "first=joe"
                params[1] == "last=cheng"
            */
    
  3. Select Control > Test Movie to test the Flash document.

    TIP

    The second parameter of the split() method defines the maximum size of the array. If you don't want to limit the size of the array created by the split() method, you can omit the second parameter.

    TIP

    The easiest way to parse a query string (a string delimited with & and = characters) is to use the LoadVars.decode() method, as shown in the following ActionScript:

    var queryStr:String = "first=joe&last=cheng&title=manager&startDate=3/6/65";
    var my_lv:LoadVars = new LoadVars();
    my_lv.decode(queryStr);
    trace(my_lv.first); // joe
    

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.