Export (0) Print
Expand All

slice Method (String)

Returns a section of a string.

function slice(start : Number [, end : Number]) : String

start

Required. The index to the beginning of the specified portion of the string.

end

Optional. The index to the end of the specified portion of the string. The returned string includes the characters up to, but not including, the character that is indicated by end. If this value is not specified, the returned string includes characters from start to the end of the string.

The slice method returns a String object containing the specified portion of the string.

The slice method copies up to, but not including, the element indicated by end. If start is negative, it is treated as length + start where length is the length of the string. If end is negative, it is treated as length + end where length is the length of the string. If end is omitted, extraction continues to the end of the string. If end occurs before start, no characters are copied to the new string.

In the following example, the first call to the slice method returns a string that contains the first five characters of str. The second call to the slice method returns a string that contains the last five characters of str.

var str = "hello world";
var firstfive = str.slice(0,5); // Contains "hello".
var lastfive = str.slice(-5);   // Contains "world".

Community Additions

ADD
Show:
© 2014 Microsoft