Export (0) Print
Expand All

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.

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

See Also

Array Object | slice Method (Array)

Applies To: String Object

Show:
© 2015 Microsoft