String.slice( ) Method | Flash 5 |
extract a substring from a string, based on positive or negative character positions |
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).
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.
A substring of string, starting at startIndex and ending at endIndex - 1, where both startIndex and endIndex are zero-relative.
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.
Note that slice( ) does not modify string; it returns a completely new string.
var fullName = "Steven Sid Mumby"; middleName = fullName.slice(7, 10); // Assigns "Sid" to middleName middleName = fullName.slice(-9, -6); // Also assigns "Sid" to middleName
String.substr( ), String.substring( ); "The slice( ) function," and "Combining String Examination with Substring Extraction," in Chapter 4