User Guide

Finding substrings and patterns in strings 215
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, in which case the character position is taken from the end
of the string, as shown in the following example:
var str:String = "Hello from Paris, Texas!!!";
trace(str.slice(11,15)); // Pari
trace(str.slice(-3,-1)); // !!
trace(str.slice(-3,26)); // !!!
trace(str.slice(-3,str.length)); // !!!
trace(str.slice(-8,-3)); // Texas
You can combine non-negative and negative integers as the parameters of the slice()
method.
Finding the character position of a matching
substring
You can use the indexOf() and lastIndexOf() methods to locate matching substrings
within a string, as the following example shows:
var str:String = "The moon, the stars, the sea, the land";
trace(str.indexOf("the")); // 10
Notice that the indexOf() method is case-sensitive.
You can specify a second parameter to indicate the index position in the string from which to
start the search, as follows:
var str:String = "The moon, the stars, the sea, the land"
trace(str.indexOf("the", 11)); // 21
The lastIndexOf() method finds the last occurrence of a substring in the string:
var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the")); // 30
If you include a second parameter with the lastIndexOf() method, the search is conducted
from that index position in the string working backward (from right to left):
var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the", 29)); // 21