Availability
JavaScript 1.2; JScript 3.0; ECMAScript v3
Synopsis
string.slice(start, end)Arguments
start
The string index where the slice is to begin. If negative, this
argument specifies a position measured from the end of the string.
That is, -1 indicates the last character, -2 indicates the second
from last character, and so on.
end
The string index immediately after the end of the slice. If not
specified, the slice includes all characters from
start to the end of the string. If this
argument is negative, it specifies a position measured from the end
of the string.
Returns
A new string that contains all the characters of
string from and including
start and up to but not including
end.
Description
slice( ) returns a string containing a slice, or
substring, of string. It does not modify
string.
The String methods slice( ), substring(
), and the deprecated substr( ) all
return specified portions of a string. slice( ) is
more flexible than substring( ) because it allows
negative argument values. slice( ) differs from
substr( ) in that it specifies a substring with
two character positions, while substr( ) uses one
position and a length. Note also that String.slice(
) is an analog of Array.slice( ).
Example
var s = "abcdefg";
s.slice(0,4) // Returns "abcd"
s.slice(2,4) // Returns "cd"
s.slice(4) // Returns "efg"
s.slice(3,-1) // Returns "def"
s.slice(3,-2) // Returns "de"
s.slice(-3,-1) // Should return "ef"; returns "abcdef" in IE 4
Bugs
Negative values for start do not work in
JScript 3.0 (Internet Explorer 4). Instead of specifying a character
position measured from the end of the string, they specify character
position 0.
See Also
Array.slice( ), String.substring( )
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.