Extracts a section of a string and returns a new string.
Method of String
Syntax
slice( beginslice[, endSlice] )
Parameters
Parameter | Description |
---|---|
beginSlice | The zero-based index at which to begin extraction. |
endSlice | The zero-based index at which to end extraction. If omitted, slice extracts to the end of the string. |
Description
slice extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.
slice extracts up to but not including endSlice. string.slice(1,4) extracts the second character through the fourth character (characters indexed 1, 2, and 3).
As a negative index, endSlice indicates an offset from the end of the string. string.slice(2,-1) extracts the third character through the second to last character in the string.
Example
The following example uses slice to create a new string.
This example produces the following result:
morning is upon