Table of Contents

String.slice( ) Method Flash 5

extract a substring from a string, based on positive or negative character positions
string.slice(startIndex, endIndex)

Arguments

startIndex

The integer position of the first character to extract from string. If startIndex is negative, the position is measured from the end of the string, where -1 is the last character, -2 is the second-to-last character, and so on (i.e., a negative startIndex specifies the character at string.length + startIndex).

endIndex

The integer position of the character after the last character to extract from string. If endIndex is negative, the position is measured from the end of the string, where -1 is the last character, -2 is the second-to-last character, and so on (i.e., a negative endIndex specifies the character at string.length + endIndex). Defaults to string.length if omitted.

Returns

A substring of string, starting at startIndex and ending at endIndex - 1, where both startIndex and endIndex are zero-relative.

Description

The slice( ) method is one of three methods that can be used to extract a substring from a string (the others being substring( ) and substr( )). The slice( ) method offers the option of using negative start and end index values, which allows us to extract a substring by measuring back from the end of a string.

Usage

Note that slice( ) does not modify string; it returns a completely new string.

Example

var fullName = "Steven Sid Mumby";
middleName = fullName.slice(7, 10);   // Assigns "Sid" to middleName
middleName = fullName.slice(-9, -6);  // Also assigns "Sid" to middleName

See Also

String.substr( ), String.substring( ); "The slice( ) function," and "Combining String Examination with Substring Extraction," in Chapter 4


Table of Contents