MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 467

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

Returning substrings
The
and
substr()
substring()
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
second parameter is the length of the substring to return, and in the
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,
character. The second method,
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
method functions similarly to the
slice()
non-negative integers as parameters, it works exactly the same. However, the
method can take negative integers as parameters.
4.
Select Control > Test Movie to test the Flash document.
You can combine non-negative and negative integers as the parameters of the
method.
slice()
You can use the
indexOf()
within a string, as shown in the following example.
methods of the String class are similar. Both return a
, returns a string 15 characters long starting from the eleventh
substr()
substring()
and
lastIndexOf()
substr()
substring()
, returns a string four characters long by
method. When given two
substring()
methods to locate matching substrings
About strings and the String class
method, the
method
slice()
467

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Flash 8

Table of Contents