User Guide

String.slice() 415
String.slice()
Availability
Flash Player 5.
Usage
my_str.slice(start:Number, [end:Number]) : String
Parameters
start
A number; the zero-based index of the starting point for the slice. If start is a negative
number, the starting point is determined from the end of the string, where -1 is the last character.
end A number; an integer that is 1+ the index of the ending point for the slice. The character
indexed by the
end parameter is not included in the extracted string. If this parameter is omitted,
String.length is used. If end is a negative number, the ending point is determined by counting
back from the end of the string, where -1 is the last character.
Returns
A string; a substring of the specified string.
Description
Method; returns a string that includes the start character and all characters up to, but not
including, the
end character. The original String object is not modified. If the end parameter is
not specified, the end of the substring is the end of the string. If the character indexed by
start is
the same as or to the right of the character indexed by
end, the method returns an empty string.
Example
The following example creates a variable, my_str, assigns it a String value, and then calls the
slice() method using a variety of values for both the start and end parameters. Each call to
the slice()
method is wrapped in a trace() statement that sends the output to the log file.
// Index values for the string literal
// positive index: 0 1 2 3 4
// string: L o r e m
// negative index: -5-4-3-2-1
var my_str:String = "Lorem";
// slice the first character
trace("slice(0,1): "+my_str.slice(0, 1)); // output: slice(0,1): L
trace("slice(-5,1): "+my_str.slice(-5, 1)); // output: slice(-5,1): L
// slice the middle three characters
trace("slice(1,4): "+my_str.slice(1, 4)); // slice(1,4): ore
trace("slice(1,-1): "+my_str.slice(1, -1)); // slice(1,-1): ore
// slices that return empty strings because start is not to the left of end
trace("slice(1,1): "+my_str.slice(1, 1)); // slice(1,1):
trace("slice(3,2): "+my_str.slice(3, 2)); // slice(3,2):
trace("slice(-2,2): "+my_str.slice(-2, 2)); // slice(-2,2):