Name

String.slice( ) Method — extract a substring from a string based on positive or negative character positions

Availability

Flash 5

Synopsis

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 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" ...

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.