Share via


slice Method (String)

Returns a section of a string.

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

Arguments

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

Remarks

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.

Example

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

Requirements

Version 3

Applies To:

String Object

See Also

Reference

Array Object

slice Method (Array)

Change History

Date

History

Reason

March 2009

Added information about the end argument.

Content bug fix.