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. IfstartIndexis 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 negativestartIndexspecifies the character atstring.length+startIndex).
- endIndex
The integer position of the character after the last character to extract from
string. IfendIndexis 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 negativeendIndexspecifies the character atstring.length+endIndex). Defaults tostring.lengthif 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" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access