Returning substrings

The substr() and substring() methods of the String class are similar. Both return a substring of a string and both take two parameters. In both methods, the first parameter is the position of the starting character in the given string. However, in the substr() method, the second parameter is the length of the substring to return, and in the substring() method the second parameter is the position of the character at the end of the substring (which is not included in the returned string). This example shows the difference between these two methods:

To find a substring by character position:

  1. Create a new Flash document and save it as substring.fla.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var myStr:String = "Hello from Paris, Texas!!!";
    trace(myStr.substr(11,15)); // Paris, Texas!!!
    trace(myStr.substring(11,15)); // Pari
    

    The first method, substr(), returns a string 15 characters long starting from the eleventh character. The second method, substring(), returns a string four characters long by grabbing all characters between the eleventh and fifteenth index.

  3. Add the following ActionScript below the code added in the preceding step:
    trace(myStr.slice(11, 15)); // Pari
    trace(myStr.slice(-3, -1)); // !!
    trace(myStr.slice(-3, 26)); // !!!
    trace(myStr.slice(-3, myStr.length)); // !!!
    trace(myStr.slice(-8, -3)); // Texas
    

    The slice() method functions similarly to the substring() method. When given two non-negative integers as parameters, it works exactly the same. However, the slice() method can take negative integers as parameters.

  4. Select Control > Test Movie to test the Flash document.

    NOTE

     

    You can combine non-negative and negative integers as the parameters of the slice() method.

You can use the indexOf() and lastIndexOf() methods to locate matching substrings within a string, as shown in the following example.

To find the character position of a matching substring:

  1. Create a new Flash document and save it as indexof.fla.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var myStr:String = "The moon, the stars, the sea, the land";
    trace(myStr.indexOf("the")); // 10
    trace(myStr.indexOf("the", 11)); // 21
    

    The first index of the word the begins at the 10th character because the indexOf() method is case sensitive; therefore the first instance of The isn't considered. You can also specify a second parameter to the indexOf() method to indicate the index position in the string from which to start the search. In the preceding code, Flash searches for the first index of the word the that occurs after the 11th character.

  3. Add the following ActionScript below the code that you added in the previous step:
    trace(myStr.lastIndexOf("the")); // 30
    trace(myStr.lastIndexOf("the", 29)); // 21
    

    The lastIndexOf() method finds the last occurrence of a substring in the string. For example, instead searching for a character or substring from the beginning of a string, lastIndexOf() starts from the end of a string and works backward. Similarly to the indexOf() method, if you include a second parameter with the lastIndexOf() method, the search is conducted from that index position, although with lastIndexOf() the string is searched backward (from right to left).

  4. Select Control > Test Movie to test your Flash document.

    TIP

     

    The indexOf() and lastIndexOf() methods are case sensitive.

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 Sample zip file and navigate to the ActionScript2.0/Strings folder to access the sample.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000951.html