JavaScript » String » slice

Syntax:
object.slice(startSlice[, endSlice])

This method is used to 'slice' a section of a string and return a new string containing that section.

This method is used to 'slice' a section of a string and return a new string containing that section. The section of the string that is sliced begins at the specified index startSlice and ends at the index before endSlice, i.e. up to but not including endSlice. A negative value can be given for endSlice, in which case the section of the string that is extracted ends at that value from the end of the original string. See the example for details.

Both examples below return the same string. The first uses a positive value for the endSlice parameter and the second a negative value.

Examples

Code:
myString = new String("Go to DevGuru.com")
slicer=myString.slice(1,8)
document.write(slicer)
Output:
o to De
Code:
myString = new String("Go to DevGuru.com")
slicer=myString.slice(1,-9)
document.write(slicer)
Output:
o to De