Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Working with Text and Strings > About strings and the String class > 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:
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.
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.
|
NOTE |
|
You can combine non-negative and negative integers as the parameters of the |
You can use the indexOf() and lastIndexOf() methods to locate matching substrings within a string, as shown in the following example.
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.
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).
|
TIP |
|
The |
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