Skip to main content

string.slice(startIndex, endIndex)

Creates a sub-string from a string

Availability

Device + Agent

Parameters

Name Type Description
startIndex Integer The string index of the sub-string’s first character
endIndex Integer Optional index of the character after the last character to be added to the sub-string

Returns

String — the specified sub-string

Description

This method copies a sequence of characters from the target string into a new string which it returns. It takes as its first argument the index of the first character to be copied from the source string. The first character in a Squirrel string is always at index zero.

Squirrel continues copying characters from the start index onward. If an end index was passed into the method’s second parameter (this is optional), Squirrel stops copying when it reaches this index. The character with the same index as the second argument is therefore not copied. If the provided end index lies beyond the end of the source string, an exception ("slice out of range") will be thrown.

If no second argument is provided, slice() copies into the new string all of the characters up to and including the last character in the source string.

Example Code